-
-
Save vlsi/aeeb4a61d9c2b67ad213 to your computer and use it in GitHub Desktop.
import java.time.Duration; | |
import java.util.Optional; | |
public class Jre7Test { | |
public static class MyClass { | |
public Optional<Duration> optional(java.time.Duration duration) { | |
return Optional.of(duration); | |
} | |
public void say(String greeting) { | |
System.out.println(greeting); | |
} | |
} | |
public static void main(String[] args) throws Throwable { | |
MyClass.class.getMethods(); | |
new MyClass().say("Works like a charm!"); | |
} | |
} |
bash-3.2$ /Library/Java/JavaVirtualMachines/jdk1.8.0_40.jdk/Contents/Home/bin/javac -source 1.7 -target 1.7 Jre7Test.java | |
warning: [options] bootstrap class path not set in conjunction with -source 1.7 | |
1 warning | |
bash-3.2$ /Library/Java/JavaVirtualMachines/jdk1.7.0_55.jdk/Contents/Home/bin/java Jre7Test | |
Exception in thread "main" java.lang.NoClassDefFoundError: java/time/Duration | |
at java.lang.Class.getDeclaredMethods0(Native Method) | |
at java.lang.Class.privateGetDeclaredMethods(Class.java:2531) | |
at java.lang.Class.privateGetPublicMethods(Class.java:2651) | |
at java.lang.Class.getMethods(Class.java:1467) | |
at Jre7Test.main(Jre7Test.java:16) | |
Caused by: java.lang.ClassNotFoundException: java.time.Duration | |
at java.net.URLClassLoader$1.run(URLClassLoader.java:366) | |
at java.net.URLClassLoader$1.run(URLClassLoader.java:355) | |
at java.security.AccessController.doPrivileged(Native Method) | |
at java.net.URLClassLoader.findClass(URLClassLoader.java:354) | |
at java.lang.ClassLoader.loadClass(ClassLoader.java:425) | |
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) | |
at java.lang.ClassLoader.loadClass(ClassLoader.java:358) | |
... 5 more | |
@vlsi Yes, I have read the spec. Did you? ;-)
12.1.1 contains more checks than 12.2 the initial class is checked more extensively. All other classes only fulfil 12.2.
Thanks for making the example fail. What you proof is that REFLECTION will have a problem, which I never doubted. I always talked about normal class loading. JDBC spec AFAIK does not mandate that reflection MUST work, does it?
Here's the full 12.1.1 from https://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html
I've split it paragraph by paragraph.
The initial attempt to execute the method main of class Test discovers that the class Test is not loaded - that is, that the Java Virtual Machine does not currently contain a binary representation for this class.
No additional checks.
The Java Virtual Machine then uses a class loader to attempt to find such a binary representation.
No additional checks.
If this process fails, then an error is thrown.
No additional checks.
This loading process is described further in §12.2.
So, the process of loading the main class is exactly the same in 12.1.1 and 12.2.
Please stop saying "contains more checks". Just point me into at least to a single "additional check".
I could easily miss it out due to English being my foreign language.
@mkarg, have you ever read the spec?
In other words, there is no difference in loading.
Especially for you, I've updated the example so you can enjoy
ClassNotFoundException: java.time.Duration
yet with no single usage of the "optional" method in question.