views:

607

answers:

4

PS: I am new to Java.

+3  A: 

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
Sorry to be unclear. I meant if there is an option to build java code to run on JRE 1.5 when compiled using JDK 1.6.
Amit Kumar
In that case use the target option on the command line. I will edit the answer accordingly.
Vincent Ramdhanie
note, doing this doesn't warn you if you use an api which only exists in java 1.6
Peter Lawrey
+2  A: 

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
+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
+1  A: 

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.
Ally Sutherland
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
+3  A: 

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.

OliBlogger
This is the only correct response.
Kevin Bourrillion
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
Are you sure you do not also need "-bootclasspath jdk1.5.0\lib\rt.jar" to prevent it from using Java 6 API?
Thilo