We've been having a problem with different versions of Java.
Basically, we have some jars that need to accessed from a system running Java version 1.3. We can't recompile all of our jars as they make use of some very useful v1.4 features (regex), so we're looking to compile some of the jars under v1.3 and some under v1.4.
Changing jre's is a pain when compiling a lot of jars at the same time, so the -target 1.3 switch looks like a good idea. Unfortunately, the code compiled still has a problem.
Here's an example of some code.
8 StringBuffer sb1 = new StringBuffer("aaa"); 9 StringBuffer sb2 = new StringBuffer("bbb"); 10 sb1.append(sb2);
And here's the error thrown when attempting to run the code with Java 1.3.
Exception in thread "main" java.lang.NoSuchMethodError at vTest.SBTest.main(SBTest.java:10)
The -target works. You might not have declared the Main-Class properly (i mean with the package). Or else it could be some kind of compatiblity problem, which is unlikely. did you have this problem when you were compiling in jdk 1.4. Why not stick onto it?
Cross-Compilation Options By default, classes are compiled against the bootstrap and extension classes of the platform that javac shipped with. But javac also supports cross-compiling, where classes are compiled against a bootstrap and extension classes of a different Java platform implementation. It is important to use -bootclasspath and -extdirs when cross-compiling; see Cross-Compilation Example below.
-target version Generate class files that will work on VMs with the specified version. The default is to generate class files to be compatible with the 1.5 VM in the JDK. When the -source 1.4 or lower option is used, the default target is 1.4. The versions supported by javac are:
1.1 Generate class files that will run on VMs in JDK 1.1 and later. 1.2 Generate class files that will run on VMs in JDK 1.2 and later, but will not run on 1.1 VMs. 1.3 Generate class files that will run on VMs in JDK 1.3 and later, but will not run on 1.1 or 1.2 VMs. 1.4 Generate class files that will run on VMs in JDK 1.4 and later, but will not run on 1.1, 1.2 or 1.3 VMs. 1.5 Generate class files that are compatible only with JDK 1.5 VMs.
-bootclasspath bootclasspath Cross-compile against the specified set of boot classes. As with the user class path, boot class path entries are separated by semicolons ( and can be directories, JAR archives, or ZIP archives.
-extdirs directories Cross-compile against the specified extension directories. Directories is a semicolon-separated list of directories. Each JAR archive in the specified directories is searched for class files.
Java Developer, Thailand
StringBuffer.append(StringBuffer sb) was introduced in 1.4. Although StringBuffer(Object o) exists, the exact used method is resolved in compile time. So compiler in JDK1.4 generates code using StringBuffer.append(StringBuffer sb) not StringBuffer.append(Object) even for target=1.3. [ August 27, 2004: Message edited by: Vlado Zajac ]
Joined: Aug 26, 2004
That's great. I used the -bootclasspath switch and pointed it at the 1.3 classes and it worked. And it also works through ant.