order of classloader in java during compile time and run time
order of classloader in java during compile time and run time
I've created below class with qualified name java.lang.String in java.
java.lang.String
package java.lang;
public class String {
public int getValue() {
return 42;
}
}
In Main Class, I have added the below code.
public class Main {
public static void main(String args) {
String s = new String();
System.out.println(s.getValue());
}
}
The code compile just fine. When I run the code, it fails with below error.
Exception in thread "main" java.lang.NoSuchMethodError: java.lang.String.getValue()I
at com.Main.main(Main.java:12)
I got to understand the java.lang.String is loaded by bootstrap classloader at runtime from rt.jar file.
java.lang.String
So, I think the order of class loading should be different during compile time and runtime. Can you please give the order of class loading during compile time and runtime.
java.lang
bootstrap then extension and then application
– Aman Chhabra
Jun 30 at 2:35
@JimGarrison True.. I also read this question twice just to be sure enough that is what has been implemented
– Aman Chhabra
Jun 30 at 2:36
This code is exact copy demonstrated on video tutorial pluralsight.com/courses/… by kevin jones. Even though he mentioned the order of classloading he didn't mention it what is order during compile time. I understand that we shouldn't create classes which are similar to one in JDK. Thanks for downvoting
– Rajkumar Natarajan
Jun 30 at 2:44
1 Answer
1
First of all, this test won't work in Java 9 or later. Attempting to compile the String class will give this error:
String
java/lang/String.java:1: error: package exists in another module: java.base
package java.lang;
^
On Java 8, I get the behavior you see. Assuming that the tweaked String class is in the same source tree, the Main class compiles, but it gives an exception when you try to run it.
String
Main
This appears to have been reported as bug 4929425. The resolution was this was a documentation bug, and they clarified the documentation for the javac command ... though maybe not enough.
javac
Anyhow, there is a difference, and it is a subtle one.
The java command simply searches in the following order:
java
The javac command first searches the source directory. If it finds a source file there, it looks for a corresponding class file in the same location and (if necessary) compiles or recompiles it. If no source file is found, then it searches the classpaths for a class file as described above for java.
javac
java
Note that it takes very careful reading of the javac manual entry to tease this out. It is easy to miss. (https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html#searching)
javac
(IMO, they could make the manual page clearer. However, this inconsistency only makes a difference if you are attempting to override some class on the bootstrap classpath or in extension directory. And you are doing it in the wrong way. Basically, this is an edge case. The problem with clearly documenting obscure edge cases is that you can end up making the documentation more confusing for the normal case.)
Actually, the
java command starts searching at the application class loader which is responsible to handle the class path, but standard class loaders are implemented by asking their parent first. But regardless of the order, classes whose qualified name starts with java. are reserved and can only be defined by the bootstrap class loader. No custom class loader or other reordering attempt could change that. In contrast, the Java programming language has no concept of class loader scopes. You can not model that runtime behavior with compile-time constructs.– Holger
yesterday
java
java.
... which basically means that edge-case inconsistencies are unavoidable.
– Stephen C
yesterday
If you want to call it that way. From the compiler’s point of view, there is no inconsistency, as it implements a behavior suitable to the programming language. The developer should not even try to integrate code supposed to be loaded by different class loaders into one compilation process. Anyway, that’s only an addendum, your answer is already sufficient.
– Holger
yesterday
(The inconsistency is between the way javac and java deal with the "situation". I'm only agreeing with you agreeing with me :-) )
– Stephen C
yesterday
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
"I've created below class with qualified name java.lang.String in java." -- THIS IS AN EXTREMELY BAD IDEA. Do not do name your class the same as an existing class; DO NOT use the
java.langpackage for your code. This will not work. Period.– Jim Garrison
Jun 30 at 2:35