Question

Is there an option to build java code to run on JRE 1.5 when compiled using JDK 1.6?

PS: I am new to Java.

Was it helpful?

Solution

If you compile your code with 1.6 then it will not run on 1.5. If you want it to run in 1.5 then you can compile the code with 1.5 and it would be able to run on both.

Try compiling with 1.5 and if there are errors then post them. The only way it will not compile on 1.5 is if you use specific 1.6 enhancements in your code.


To answer the real question.

  javac -target 1.5

See here for more details.

OTHER TIPS

Yes, you can. See http://java.sun.com/javase/6/docs/technotes/tools/windows/javac.html and look for the section "Cross-Compilation Options". In short, you need to specify -target=1.5 to javac. Ant also supports this flag, of course.

Have a look at the javac "-source" and "-target" options:

http://java.sun.com/javase/6/docs/technotes/tools/windows/javac.html

-source release

Specifies the version of source code accepted. The following values for release are allowed:

  • 1.3 The compiler does not support assertions, generics, or other language features introduced after JDK 1.3.
  • 1.4 The compiler accepts code containing assertions, which were introduced in JDK 1.4.
  • 1.5 The compiler accepts code containing generics and other language features introduced in JDK 5.
  • 5 Synonym for 1.5.
  • 1.6 This is the default value. No language changes were introduced in Java SE 6. However, encoding errors in source files are now reported as errors, instead of warnings, as previously.
  • 6 Synonym for 1.6.

+

-target version

Generate class files that target a specified version of the VM. Class files will run on the specified target and on later versions, but not on earlier versions of the VM. Valid targets are 1.1 1.2 1.3 1.4 1.5 (also 5) and 1.6 (also 6).

The default for -target depends on the value of -source: - If -source is not specified, the value of -target is 1.6

  • If -source is 1.2, the value of -target is 1.4
  • If -source is 1.3, the value of -target is 1.4
  • For all other values of -source, the value of -target is the value of -source.

It all depends on what APIs you are using. Things like Swing, Instrumentation, JConsole etc change over time.

If you try:

http://www.coderanch.com/t/382318/Java-General/java/New-Features-Java

it has links to the pages indicating the differences between each of the last major versions, with:

http://java.sun.com/javase/6/webnotes/features.html

being a list of the changed/new features in the latest version.

Hopefully that'll give you some idea.

And of course then you'll need to compile it under 1.5 to get it to run with that JRE.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top