File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes How to choose java version at run time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to choose java version at run time" Watch "How to choose java version at run time" New topic

How to choose java version at run time

Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270
Hi All,

I have both Java 1.4.2 and Java 1.5 installed on my Windows XP machine.
How can I choose which version to use at run time? I have some applications which will run on Java 1.4 only.

Sanjaya Sugiarto
Ranch Hand

Joined: Mar 25, 2004
Posts: 229
Just adjust the classpath definition.

If you use Windows, than the classpath can be found:
start -- settings -- system -- advanced -- environment variables than adjust the CLASSPATH definition.
After that, you can check it: open the console, and type: java -version
It will show the actual java version your system run

<a href="" target="_blank" rel="nofollow">Business Information Technology - Hochschule Furtwangen University, Germany</a>
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Note that the CLASSPATH setting will have no effect on which version of the runtime is used.

What you want to set is your PATH environment variable.
It can be as simple as set PATH=C:\j2sdk1.4.2\bin;%PATH% for 1.4 or you might set it permanently in the Windows dialog.

To test which version JVM will be started, use:
java -version
To test which version compiler will be started, use:
javac -J-version
To determine the version at runtime, use:
String version = System.getProperty("java.version");

Tony Morris
Java Q&A (FAQ, Trivia)
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
In Windows, I have a batch file which has all of my jvm's listed. All are REMarked out except the one I want to run with. For example, to test myProgram, a jar file, in a JVM of my choice, I just remove the REM from the version of the JVM I want to use:

At the command prompt I just enter the name of the batch file to run:

Now it will launch in 1.4.2_04. No problem.

Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
As Jeff shows, the best way to ensure which version of Java you run is by specifying the full path to the java executable. If you don't like the command line, I think you can set up your IDE to run with several different versions of the JDK. Unfortunately, I don't know the details as I haven't tried to do this before.


p.s. If you write and compile your programs with JDK 1.4, they should still run on JDK 1.5 as-is. However, the opposite isn't true unless you use the "-target 1.4" option.

Java API Documentation
The Java Tutorial
I agree. Here's the link:
subject: How to choose java version at run time
It's not a secret anymore!