views:

164

answers:

5

I've compiled my source with java version 1.6 using the parameters -source 1.5 and -target 1.5, and the compiler doesnt complain at all.

Still, the application won't run with java 1.5 due to missing methods. Ofcourse I could rewrite some of my source code to be 1.5 compliant, but what I don't understand is; shouldn't the java bytecode in the bottom be "frontwards" compliant?

Aren't the methods converted into bytecode? Is it possible to compile the 1.6 libs/methods (formely String.isEmpty()) to 1.5 bytecode and pack it all into the archive?

+1  A: 

I don't believe java will recompile the native java code backwards. So, if you make a 1.6 call - you will not be able to access it in 1.5

Dave Morgan
You can if the JRE you are running on is 1.6, it all depends on which JRE is running your code--it is providing the libraries.
Bill K
+6  A: 

If you mean base Java library methods, then no, those methods are not converted to byte code when you compile; they've already been compiled to byte-code by Sun (or the third-party JVM distributer) and installed on your operating system. They are referenced and used by your compiled code.

Randolpho
+1  A: 

You can change the library you are compiling against to be an older library. In packages like eclipse, each installed JDK should appear in a "Select library" window, you can choose which one you wish to compile against.

If not, you should be able to override it in your ant file or CLI compile command.

If targeting an older JVM, this really has to be done or you may use calls that will not be available.

Bill K
+3  A: 

The full set of command line options you need are:

java -source 1.5 -target 1.5 -bootclasspath /usr/jdk/jdk1.5.0_17/jre/lib/rt.jar

(Change bootclasspath to however your machine is setup.)

Of course, APIs enhancements in 1.6 will not be in 1.5. 1.5 is most of its way through its End of Service Life period, so you might want to consider a 1.6 minimum anyway.

Tom Hawtin - tackline
Only problem, Mac OSX, which says about itself its the only OS with fully integrated java, doesnt support 1.6 :/
Viktor Sehr
1.6 is certainly available for Mac OS X. I believe they even have a version of the 6u10 plugin now. What I believe they don't yet, unfortunately, is 1.6 by default across all their supported versions of Mac OS X.
Tom Hawtin - tackline
I think its only available in 64 bit versions
Viktor Sehr
+1  A: 

The source parameter only makes the compiler check at a language syntax level (source=1.4 would for example complain if it encounters generics) but won't restrict you to only using APIs available in the specified Java version.

The target parameter will make the compiler output class files that can be used by a runtime of the specified version but won't (just like -source) validate any API conformity.

henrik