File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How can we change tha java version to use? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How can we change tha java version to use?" Watch "How can we change tha java version to use?" New topic
Author

How can we change tha java version to use?

Anas Nashroel
Greenhorn

Joined: May 21, 2008
Posts: 12
Hi, I installed jdk 1.4 and 1.5 on windows. I set JAVA_HOME to refer to jdk 1.5 installation folder. When I type java -version, it refers to jdk 1.5 installation.
and then, I downloaded and installed jdk 1.6. When the installation was finished, I type java -version in command prompt, and it refers to jdk 1.6 version. But strangetly, I looked at path environment variables, and I couldn't find path that leads to jdk 1.6 installation folder. The path is to jdk 1.5 installation folder.
So, how can i set the java version to use in windows ??
Thanks in advance
Paul Campbell
Ranch Hand

Joined: Oct 06, 2007
Posts: 338
Originally posted by Anas Nashroel:
Hi, I installed jdk 1.4 and 1.5 on windows. I set JAVA_HOME to refer to jdk 1.5 installation folder. When I type java -version, it refers to jdk 1.5 installation.
and then, I downloaded and installed jdk 1.6. When the installation was finished, I type java -version in command prompt, and it refers to jdk 1.6 version. But strangetly, I looked at path environment variables, and I couldn't find path that leads to jdk 1.6 installation folder. The path is to jdk 1.5 installation folder.
So, how can i set the java version to use in windows ??
Thanks in advance


In your windows environment variables:

Change CLASSPATH to be your 1.6 installation path... likely something like "C:\Program Files\Java\jdk1.6.0_06\bin" <== don't forget the quotes.

If you do not have a CLASSPATH environment variable, create one.

Change your PATH variable to have %CLASSPATH%; as the first entry.

You will likely need to reboot before the variables become useful.

But first I would uninstall your 1.4 and 1.5 installations and compile with the source options javac -source 1.4 when you want use 1.4 and javac -source 1.5 when you want to compile as the 1.5 jdk.

[ October 14, 2008: Message edited by: Paul Campbell ]
[ October 14, 2008: Message edited by: Paul Campbell ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Change CLASSPATH

Isn't it PATH ?


[My Blog]
All roads lead to JavaRanch
Paul Campbell
Ranch Hand

Joined: Oct 06, 2007
Posts: 338
Originally posted by Christophe Verre:

Isn't it PATH ?


The PATH variable is one windows environment variable (the main search path)... if you create a CLASSPATH environment variable you can use the %CLASSPATH% in your PATH variable... why do that instead of just doing the PATH variable? I just really prefer touching the path variable as little as possible and avoid modifying it (other than adding the %CLASSPATH% system variable).

I should update my original post for clarity though... and in the vein of a picture is worth a thousand words...



[ October 14, 2008: Message edited by: Paul Campbell ]
[ October 14, 2008: Message edited by: Paul Campbell ]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
The PATH variable tells the operating system where to look for executable files.
The CLASSPATH variable tells the Java compiler and JRE where to look for class files.
They are totally unrelated - putting the path to the JDK bin directory in the CLASSPATH will not tell the OS where to find the javac program.


Joanne
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Originally posted by Anas Nashroel:
I looked at path environment variables, and I couldn't find path that leads to jdk 1.6 installation folder. The path is to jdk 1.5 installation folder.
So, how can i set the java version to use in windows ??
Thanks in advance


When you say you looked at the environment variables, did you echo the PATH variable to a command prompt or did you look at the environment variables in the system properties dialog ? If the latter did you check in both the user and system variables ? It's possible to set a PATH variable in both which Windows then combines to form the complete PATH variable.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
Joanne is right (as usual); you don't need to do anything with your CLASSPATH, at least if you are using a recent version of Java.
Probably the easiest way to use a particular version of Java is to set the PATH from the shell/terminal/command line; there are old posts telling you how to do it here and here.
Put the PATH to the version of Java you wish to use first; remember it starts with the drive letter (often C) on Windows and / on Unix/Linux and ends with bin. That way your new path PATH will only apply to that one shell/terminal/command line.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

putting the path to the JDK bin directory in the CLASSPATH will not tell the OS where to find the javac program.

Exactly.
Paul Campbell
Ranch Hand

Joined: Oct 06, 2007
Posts: 338
Originally posted by Joanne Neal:
The PATH variable tells the operating system where to look for executable files.
The CLASSPATH variable tells the Java compiler and JRE where to look for class files.
They are totally unrelated - putting the path to the JDK bin directory in the CLASSPATH will not tell the OS where to find the javac program.


I totally agree... and adding %CLASSPATH% as the first entry in the PATH variable does that.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
The CLASSPATH is where the JVM looks for other executable .class files, so you don't want to confuse CLASSPATH and PATH.
Last thing you want in your PATH is your CLASSPATH.
Paul Campbell
Ranch Hand

Joined: Oct 06, 2007
Posts: 338
Originally posted by Campbell Ritchie:
The CLASSPATH is where the JVM looks for other executable .class files, so you don't want to confuse CLASSPATH and PATH.
Last thing you want in your PATH is your CLASSPATH.


Point taken... after reading and re-reading... I get where I had this wrong. Thank you.
[ October 15, 2008: Message edited by: Paul Campbell ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
Originally posted by Paul Campbell:
Point taken...



You have to be very wary about saying CLASSPATH; every time you do hundreds of greenhorns go changing their computer's environment setting unnecessarily.
Harish Vembu
Ranch Hand

Joined: Jul 19, 2004
Posts: 33


Though the answers to the original post were given, I am not able to get the desired Java version somehow.


I have installed JDK 1..6 and JDK 1.4 on my windows XP machine on different paths

Here is what I have in my environment variables

PATH : C:\Program Files\PHP\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:
\WINDOWS\system32\WindowsPowerShell\v1.0;C:\j2sdk1.4.2_19\bin;%ANT_HOME%\bin;C:\
Program Files\PHP\php.ini;C:\j2sdk1.4.2_19\bin;C:\ant-1.5.2\bin;

I have the highlighted the one for JDK 1.4 bin directory in path.

I have JAVA_HOME user variable set to C:\jdk1.6.0_23 which I am not using in PATH.

When I use Java -version I always get version 6 regardless of me changing the PATH to respective JDK bin directory.

I am wary of setting CLASSPATH variable in PATH as advised on this forum.


Please clarify. Appropriate reply would be much appreciated.


Thanks & Best Regards

Harish


Harish
SCJP ,SCDJWS
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

That's because it's finding C:\WINDOWS\system32\java.exe first. This is a wrapper that uses the registry to find and launch your Java 6 JVM.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
Move your Java™ installation folders earlier in the PATH string than the System folder Rob mentioned.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
Another way to do it is to invoke java with its full PATH
C:\java\myPrograms:> C:\j2sdk1.4.2_19\bin\java Foo
If there are spaces in the PATH, you may need quotes "" around the C;\...\java String.
Harish Vembu
Ranch Hand

Joined: Jul 19, 2004
Posts: 33


Thanks Rob and Ritchie. Your answers are spot on.

It really helped.

I was also thinking on those lines of system 32 which is causing to always use JDK 6 version.

I prefixed the path environment variable with JDK 1.4 path and it worked.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
You're welcome
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
Compsci Student,
Your post was moved to a new topic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can we change tha java version to use?
 
Similar Threads
Trouble getting off the ground with JBoss 5.1
Need some Guidance in using Java 1.6 with ANT
How I can use higher than 1.4 version java in IBM RAD.
How to use the JRE I want, to launch my program when I have two JDKS installed?
Trouble installing JDK 7