Skip to content

Instantly share code, notes, and snippets.

@vlsi
Last active August 29, 2015 14:23
Show Gist options
  • Save vlsi/aeeb4a61d9c2b67ad213 to your computer and use it in GitHub Desktop.
Save vlsi/aeeb4a61d9c2b67ad213 to your computer and use it in GitHub Desktop.
JRE 7 compatibility test
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
Copy link
Author

vlsi commented Jun 18, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment