wood burning stoves 2.0*
The moose likes Java in General and the fly likes Problem with javac -target Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem with javac -target" Watch "Problem with javac -target" New topic
Author

Problem with javac -target

Barry Harvey
Greenhorn

Joined: Aug 26, 2004
Posts: 4
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)

Does -target work or am I being stupid?
Shashank Agarwal
Ranch Hand

Joined: May 20, 2004
Posts: 105
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?
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312

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.



SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
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 ]
Barry Harvey
Greenhorn

Joined: Aug 26, 2004
Posts: 4
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.

Thanks for the help
 
Don't get me started about those stupid light bulbs.
 
subject: Problem with javac -target