Wednesday, December 5, 2007

A little bit of 'target jsr14'

I was taking a look at the Mule's CXF transport which requires Java 5.0. While doing that, I ranted about the annotations feature to one of my colleagues (I simply do not like the idea that having "@Override" in my code stops my 1.4 compiler, why not "// @Override").

I then read about this article -- Using Java 5 language features in earlier JDKs which mentions about the jsr14 (oh, I haven't tried it before, really). Specifying "jsr14" as your target VM version is a workaround of having 1.5 source to run on earlier JVM, however, it is not recommended by Sun, not supported, and not documented. Refer to this document for supported target values.

For that case, I wasn't too surprised that I can't read much about its details with a few Google search attempts.

These are what I have tried:

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class Source{

private List foo = null;

public List getFoo(){
return this.foo;
}

public void setFoo(List foo){
this.foo = new ArrayList(foo);
}

@Deprecated
public String toString(){
return "Foobar always!";
}

public static void main(String[] args)
throws Exception{

Method method = Source.class.getMethod("toString", new Class[]{});
if(method.isAnnotationPresent(Deprecated.class)){
System.out.println("toString is deprecated! :-|");
}

List foo = new Vector();
foo.add("Foo");
foo.add("Bar");

Source src = new Source();
src.setFoo(foo);

for(String fooElement: src.getFoo()){
System.out.println(fooElement);
}
}
}


Deprecated has a @Retention(RetentionPolicy.RUNTIME) (see An Overview of Java 5's Annotations). Then, compiled with 1.5:

/opt/java1.5/bin/javac -verbose Source.java -source 1.5 -target 1.5
[parsing started Source.java]
[parsing completed 39ms]
[search path for source files: [.]]
[search path for class files: [/usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar, /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/jsse.jar, /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/jce.jar, /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/charsets.jar, /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/ext/sunjce_provider.jar, /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/ext/localedata.jar, /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/ext/sunpkcs11.jar, /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/ext/dnsns.jar, .]]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/reflect/Method.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/util/ArrayList.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/util/List.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/util/Vector.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/Object.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/String.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/Exception.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/Deprecated.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/annotation/Annotation.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/annotation/Retention.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/annotation/RetentionPolicy.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/annotation/Target.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/annotation/ElementType.class)]
[checking Source]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/util/Collection.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/util/AbstractList.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/util/AbstractCollection.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/Iterable.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/Throwable.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/Class.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/NoSuchMethodException.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/SecurityException.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/reflect/AccessibleObject.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/System.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/io/PrintStream.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/io/FilterOutputStream.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/io/OutputStream.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/Error.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/RuntimeException.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/util/Iterator.class)]
[wrote Source.class]
[total 335ms]


Running using 1.4:

/opt/j2sdk1.4/bin/java Source
Exception in thread "main" java.lang.UnsupportedClassVersionError: Source (Unsupported major.minor version 49.0)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:539)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)


Recompile with jsr14:

/opt/java1.5/bin/javac -verbose Source.java -source 1.5 -target jsr14
[parsing started Source.java]
[parsing completed 47ms]
[search path for source files: [.]]
[search path for class files: [/usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar, /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/jsse.jar, /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/jce.jar, /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/charsets.jar, /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/ext/sunjce_provider.jar, /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/ext/localedata.jar, /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/ext/sunpkcs11.jar, /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/ext/dnsns.jar, .]]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/reflect/Method.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/util/ArrayList.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/util/List.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/util/Vector.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/Object.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/String.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/Exception.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/Deprecated.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/annotation/Annotation.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/annotation/Retention.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/annotation/RetentionPolicy.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/annotation/Target.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/annotation/ElementType.class)]
[checking Source]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/util/Collection.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/util/AbstractList.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/util/AbstractCollection.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/Iterable.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/Throwable.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/Class.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/NoSuchMethodException.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/SecurityException.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/reflect/AccessibleObject.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/System.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/io/PrintStream.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/io/FilterOutputStream.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/io/OutputStream.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/Error.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/RuntimeException.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/NoClassDefFoundError.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/LinkageError.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/lang/ClassNotFoundException.class)]
[loading /usr/lib/jvm/java-1.5.0-sun-1.5.0.13/jre/lib/rt.jar(java/util/Iterator.class)]
[wrote Source.class]
[total 366ms]


Re-run with 1.4:

/opt/j2sdk1.4/bin/java Source Exception in thread "main" java.lang.NoClassDefFoundError
at Source.class$(Source.java:26)
at Source.main(Source.java:27)
Caused by: java.lang.ClassNotFoundException: java.lang.Deprecated
at java.net.URLClassLoader$1.run(URLClassLoader.java:199)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:141)
... 2 more


Ouchhh :(

Get rid of Deprecated.class from code, recompile and run:

/opt/j2sdk1.4/bin/java Source
Foo
Bar


Pfffft? ;)

- yc

No comments: