Skip to content

Instantly share code, notes, and snippets.

@vlsi

vlsi/Jre7Test.java

Last active Aug 29, 2015
Embed
What would you like to do?
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
@mkarg

This comment has been minimized.

Copy link

@mkarg mkarg commented Jun 17, 2015

You got fooled by the fact that there is a difference between loading the very initial main class at JVM bootstrap, and loading ANY OTHER subsequent class. This is difference is clearly documented in chapters JLS 12.1 (initial class) and JLS 12.2 (other classes).

After correcting your example it works pretty well, see: https://gist.github.com/mkarg/88a89ae0dbffcfb7543e

@vlsi

This comment has been minimized.

Copy link
Owner Author

@vlsi vlsi commented Jun 17, 2015

@mkarg, have you ever read the spec?

12.1.1. Load the Class Test
This loading process is described further in §12.2.

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.

@mkarg

This comment has been minimized.

Copy link

@mkarg mkarg commented Jun 17, 2015

@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?

@vlsi

This comment has been minimized.

Copy link
Owner Author

@vlsi 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
You can’t perform that action at this time.