views:

224

answers:

4

Is there any way to restrict a java project configured with 1.4 compiler compliance level from using 1.5/1.6 specific API where the system is playing with 1.6 JRE?

Setting only 1.4 compiler compliance level on my project does not warn me from using especially 1.5/1.6 specific java.lang.String methods.

+2  A: 

I would suggest the safest (and perhaps the simplest) way is to compile using a JDK 1.4, and run using a JRE 1.6.

Brian Agnew
A: 

What I would do is to remove those JDKs/JREs, which you don't want to use, from Eclipse's settings. In your case that would be 1.5 and 1.6.

To do this, go to Windows->Preferences and then type "JRE" in the top left hand side.

I would also check which JRE Eclipse in launched in and maybe edit your eclipse.ini.

Lenni
+6  A: 

Make sure that you're also using an 1.4 JRE system library in your project. The 1.4 compiler settings change the features which will be written into the class files, it doesn't restrict what you can see from the libraries in your project.

Open the properties for your project and check what you find under "Java Build Path" -> "Libraries" -> "JRE System Library". Click "Edit" to change or add new Java versions.

Aaron Digulla
So, there is no way to dictate the compiler to throw error/warning for using 1.5/1.6 APIs with the JRE 1.6 System library included in the 1.4 project's classpath?
raatprohory
Of course not. From the view of the compiler, rt.jar is just another JAR on the classpath. It doesn't "know" that the classes in there mean something special to you.
Aaron Digulla
+1  A: 

You will need the rt.jar from a 1.4 JRE (for instance by having the 1.4 JRE installed). If using javac use the following options:

 javac -source 1.4 -target 1.4 -bootclasspath /path/to/j2se1.4/lib/rt.jar [...]

Using javac from a later JRE means that it should have fewer bugs for old source, although it may not necessarily be entirely "bug compatible".

You might also want to use -Djava.ext.dirs=directories and -Djava.endorsed.dirs, but putting things in those directories is generally a bad idea.

Note: Even 1.5 has finished its End Of Service Life period. Get with 1.6! (Or buy one of our excellent Java for Business contracts...)

Tom Hawtin - tackline