tags:

views:

344

answers:

7
+2  Q: 

jdk versions

i develop java app in 2 different computers ( same souce-code) , which have different jdk versions. is there a way to specify jdk version in the source-code so that ide (e.g netbeans) compile the same source-code on both machines. To be clearer: jdk5: jdbc-interface doesnt support some methods (e.g NClob, XML...) which are available in jdk6, code gets compiled well on machine with jdk6, not on machine with jdk5, so i have to comment jdk6-only stuffs out when working on jdk5-machine

thanx

+6  A: 

Not in the source code. You want to set your IDE to compile using a Java 5 JDK. You can do this by installing a Java 5 JDK alongside the Java 6 JDK, and specifying that (which is IDE dependent).

Alternatively, if you're using Ant, you can specify the Java version in the javac task.

Brian Agnew
+1  A: 

You'll always have to regress to and code against the lowest version of the JDK - in your case you can't use the features introduced in JDK version 6.

As Brian has said, it's probably safest if you work with the IDE configured to use the same JDK (version 5 in your case).

You can switch to a particular runtime version using the version argument, but that's probably not going to help in you.

Also note, compiling your code against version 6 and running against version 5 will cause runtime error due the incompatible class versions, when as you stated you try to use JDBC methods that didn't exist in the older version.

Nick Holt
A: 

In NetBeans you have right click on project properties then source version if you have jdk 1.6 you can build it with previous versions

Mite Mitreski
That won't help you if you use API methods that don't exist in the previous version.
Michael Borgwardt
+4  A: 

Don't overcomplicate the issue... install the same version of the JDK on all the machines, and set your IDEs to use that version. Ignore other versions.

skaffman
A: 

I know this has been answered but if you cant update the 1.5 to 1.6 then all you need to do is set the ide that is using 1.6 to compile to "source compatibility" of 1.5

There is no need to install the 1.5 on the machine that is running 1.6 because the JDK is backward compatible.

The setting in eclipse is preferences > Java > Compiler

Shawn
A: 

What you need to do is specify the source compatibility to something that's supported by both JDKs (<= 1.5). It may make sense to go lower than 1.5 if, for instance the application you're building may be deployed to an environment that only has Java 1.3 installed.

I know this is possible in Eclipse, but unfortunately, I'm not familiar with NetBeans, but I'm sure it would be possible too.

If it's any help, here's how you do it if you're just running javac from the console (assuming you want 1.5 as your base level):

javac -source 1.5 ...

This is also possible as a plug in configuration within a Maven POM, if you're using Maven (which I understand NetBeans has excellent support for). This is how you'd do that (from http://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html):

<project>
  [...]
  <build>
    [...]
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.4</source>
          <target>1.4</target>
        </configuration>
      </plugin>
    </plugins>
    [...]
  </build>
  [...]
</project>

And as Brian Agnew points out, possible in Ant also.

Jack Leow
A: 

Problem is: do you need to support JVM 1.5? If so, your code should be fully 1.5-compatible (which doesn't seem to be). If not, just update your JVM in all your machines (as already suggested) to same JVM version (and don't forget version minor: 1.6.0_03 != 1.6.0_14).

ignasi35