| Summary: | [Compiler] Code compiled against JDK 1.5 does not run in jdk 1.4 | ||
|---|---|---|---|
| Product: | [Eclipse Project] JDT | Reporter: | Jim Adams <Jim.Adams> |
| Component: | Core | Assignee: | Philipe Mulet <philippe_mulet> |
| Status: | RESOLVED DUPLICATE | QA Contact: | |
| Severity: | normal | ||
| Priority: | P3 | ||
| Version: | 3.0 | ||
| Target Milestone: | 3.0 M8 | ||
| Hardware: | PC | ||
| OS: | Windows XP | ||
| Whiteboard: | |||
|
Description
Jim Adams
Please provide steps to reproduce. Also to be a fair comparison, you should try to achieve the result with javac 1.4, as the Eclipse compiler you are using is a 1.4 compiler (moving to 1.5, but not yet there). Install the latest 1.5 beta
Set Eclipse to use the 1.5 JRE in the installed JREs
ALso set it to use the 1.4.2_02 jre (which 1.4 version shouldn't matter)
Create the following file in Eclipse
public class StringBufferTest
{
public static void main(String[] args)
{
StringBuffer a = new StringBuffer();
a.append("This is a test");
System.out.println(a.toString());
System.exit(0);
}
}
Build and run it.
Now switch the runtime VM to 1.4 in the run... dialog.
Re run without rebuilding
It fails.
Below is the output of my sessions outside eclipse from this point on. As you
can see before rebuilding it fails. After rebuilding it works. Note that I did
have to use the -target 1.4 switch on the compiler but Eclipse doesn't have
such a switch to my knowledge.
C:\eclipse\workspace\test>dir
Volume in drive C is WXP
Volume Serial Number is 08E8-C1C4
Directory of C:\eclipse\workspace\test
02/24/2004 04:05 PM <DIR> .
02/24/2004 04:05 PM <DIR> ..
02/24/2004 03:57 PM 226 .classpath
02/24/2004 03:57 PM 380 .project
02/24/2004 04:05 PM 804 StringBufferTest.class
02/24/2004 03:58 PM 552 StringBufferTest.java
4 File(s) 1,962 bytes
2 Dir(s) 1,767,754,752 bytes free
C:\eclipse\workspace\test>java -classpath . StringBufferTest
This is a test
C:\eclipse\workspace\test>java -version
java version "1.5.0-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta-b32c)
Java HotSpot(TM) Client VM (build 1.5.0-beta-b32c, mixed mode)
C:\eclipse\workspace\test>"c:\Program Files\java\j2re1.4.2_02\bin\java" -
classpa
th . StringBufferTest
Exception in thread "main" java.lang.NoSuchMethodError:
java.lang.StringBuffer.a
ppend(Ljava/lang/String;)Ljava/lang/AbstractStringBuilder;
at StringBufferTest.main(StringBufferTest.java:19)
C:\eclipse\workspace\test>c:\jdk1.5.0\bin\javac -classpath .
StringBufferTest.ja
va
C:\eclipse\workspace\test>java -classpath . StringBufferTest
This is a test
C:\eclipse\workspace\test>"c:\Program Files\java\j2re1.4.2_02\bin\java" -
classpa
th . StringBufferTest
Exception in thread "main" java.lang.UnsupportedClassVersionError:
StringBufferT
est (Unsupported major.minor version 49.0)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
C:\eclipse\workspace\test>c:\jdk1.5.0\bin\javac -target 1.4 -classpath .
StringB
ufferTest.java
C:\eclipse\workspace\test>"c:\Program Files\java\j2re1.4.2_02\bin\java" -
classpa
th . StringBufferTest
This is a test
C:\eclipse\workspace\test>java -classpath . StringBufferTest
This is a test
C:\eclipse\workspace\test>c:\jdk1.4.2_02\bin\javac -classpath .
StringBufferTest
.java
The system cannot find the path specified.
C:\eclipse\workspace\test>c:\j2sdk1.4.2_02\bin\javac -classpath .
StringBufferTe
st.java
C:\eclipse\workspace\test>java -classpath . StringBufferTest
This is a test
C:\eclipse\workspace\test>"c:\Program Files\java\j2re1.4.2_02\bin\java" -
classpa
th . StringBufferTest
This is a test
Note that in your script, it shows that when using javac 1.4 you did compile against 1.4 libraries (by default). Yes, but when I compiled against 1.5, I ran against 1.4 libraries with no problem (using the javasoft compiler) Understood, but your point was to use a 1.4 compiler to compile against JDK1.5 and then run against 1.4. Javac doesn't support it, as it refuses to compile against more recent libraries. We may still have an obvious issue leading us to misbehave, will investigate. I recompiled the classes, first with Eclipse and then with jdk 1.5. I dumped
out each class and Eclipse has compiled in the return type differently.
C:\eclipse\workspace\test>c:\jdk1.5.0\bin\javap -classpath . -c
StringBufferTest
Compiled from "StringBufferTest.java"
public class StringBufferTest extends java.lang.Object{
public StringBufferTest();
Code:
0: aload_0
1: invokespecial #9; //Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: new #17; //class java/lang/StringBuffer
3: dup
4: invokespecial #18; //Method java/lang/StringBuffer."<init>":()V
7: astore_1
8: aload_1
9: ldc #20; //String This is a test
11: invokevirtual #24; //Method java/lang/StringBuffer.append:
(Ljava/lang/
String;)Ljava/lang/AbstractStringBuilder;
14: pop
15: getstatic #30; //Field java/lang/System.out:Ljava/io/PrintStream;
18: aload_1
19: invokevirtual #34; //Method java/lang/StringBuffer.toString:()
Ljava/la
ng/String;
22: invokevirtual #40; //Method java/io/PrintStream.println:
(Ljava/lang/St
ring;)V
25: iconst_0
26: invokestatic #44; //Method java/lang/System.exit:(I)V
29: return
}
C:\eclipse\workspace\test>c:\jdk1.5.0\bin\javac -target 1.4 -classpath .
StringBufferTest.ja
va
C:\eclipse\workspace\test>c:\jdk1.5.0\bin\javap -classpath . -c
StringBufferTest
Compiled from "StringBufferTest.java"
public class StringBufferTestextends java.lang.Object{
public StringBufferTest();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: new #2; //class java/lang/StringBuffer
3: dup
4: invokespecial #3; //Method java/lang/StringBuffer."<init>":()V
7: astore_1
8: aload_1
9: ldc #4; //String This is a test
11: invokevirtual #5; //Method java/lang/StringBuffer.append:
(Ljava/lang/S
tring;)Ljava/lang/StringBuffer;
14: pop
15: getstatic #6; //Field java/lang/System.out:Ljava/io/PrintStream;
18: aload_1
19: invokevirtual #7; //Method java/lang/StringBuffer.toString:()
Ljava/lan
g/String;
22: invokevirtual #8; //Method java/io/PrintStream.println:
(Ljava/lang/Str
ing;)V
25: iconst_0
26: invokestatic #9; //Method java/lang/System.exit:(I)V
29: return
}
|