6

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.

amit kumar
  • 20,438
  • 23
  • 90
  • 126

4 Answers4

10

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.

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
10

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.

OliCoder
  • 1,370
  • 10
  • 25
  • 1
    Note also that it is preferred to "just compile using JDK 1.5", as the newer compiler has bug fixes and performance improvements. – Kevin Bourrillion Dec 18 '09 at 19:05
  • 2
    Are you sure you do not also need "-bootclasspath jdk1.5.0\lib\rt.jar" to prevent it from using Java 6 API? – Thilo Dec 23 '09 at 05:57
6

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.
Alistair Sutherland
  • 1,381
  • 13
  • 10
  • 3
    But in addition to that, the class libraries are also different. So if you want to be sure that it will work on Java5, use the JDK5, no just the JDK6 with --source 6. – Thilo Dec 18 '09 at 12:10
4

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.

Mark Mayo
  • 12,230
  • 12
  • 54
  • 85
  • 3
    +1 for API's. Noone else has mentioned this and it can have an impact on your code if you coded to an API that has changed between 1.5 and 1.6. The -source and -target options will not help in this case. – Robin Dec 18 '09 at 14:33