It seems that the java-task doesn't initialize the class the same way as the java tool. Here is a use case: u.A and t.B are two classes. main is defined only in A. java t.B should call t.B class initialisation but when used inside a ant buildfile it doesn't work this way : --------------------------------------------------------------- U:\test>ant Buildfile: build.xml compile: run: A CLASS INITIALIZATION<<<<<< hey what about B! MAIN I am a A. all: BUILD SUCCESSFUL Total time: 1 second U:\test>java -classpath build t.B A CLASS INITIALIZATION B CLASS INITIALIZATION<<<< Ok that is better! MAIN I am a B. Here is a list of files and contents : build.xml ------------------------------- <?xml version="1.0" encoding="ISO-8859-1"?> <project name="test" default="all"> <target name="all" depends="run" /> <target name="run" depends="compile"> <java className="t.B"> <classpath> <pathelement path="build"/> </classpath> </java> </target> <target name="compile"> <javac destdir="build" srcdir="."> <patternset> <include name="**/*.java"/> </patternset> <classpath> <pathelement path="."/> </classpath> </javac> </target> <target name="init"> <mkdir dir="build"/> </target> </project> ------------------------------ t/B.java: ------------------------------ package t; public class B extends u.A { static { System.out.println("B CLASS INITIALIZATION"); setA(new B()); } public String toString() { return "I am a B."; } } ------------------------------ u/A.java: ------------------------------ package u; public class A { public static void main(String [] args){ System.out.println("MAIN"); System.out.println(a); } static A a=new A(); static { System.out.println("A CLASS INITIALIZATION"); } protected static void setA(A oa) { a=oa; } public String toString() { return "I am a A."; } } Any clue of what is wrong?
Created attachment 94 [details] a jar with the test case.
*** This bug has been marked as a duplicate of 1613 ***
Using IBM JDK 1.1.8, even if I invoke java -classpath build t.B from the commandline, it still doesn't print out "B CLASS INITIALIZATION". In other words, IBM's JDK1.1.8 implementation of static initialization is flawed, IMO.
In IBM JDK 1.3, it works as it should on the commandline. i.e., it prints "B CLASS INITIALISATION" properly. Would it be a good stand to take if we say Ant's <java> task will work only as good as the JVM's java task?