Click to See Complete Forum and Search --> : "java -version" doesn't display proper value.


goldest
October 31st, 2009, 09:35 AM
Hi,

I am new to java and facing a strange problem regarding java version. I have 1.4 and 1.5 both the JDKs installed on my Windows XP machine.

When I open command prompt and try to figure out what java version I have currently, it always gives me version 1.5. Even though when I set my path to “C:\j2sdk1.4.2_12\bin” and JAVA_HOME to “C:\j2sdk1.4.2_12”, this problem still persists. I wonder what I exactly need to do so that when I set my path and JAVA_HOME to 1.4 JDK it will reflect the same in “java –version” command on the command prompt.

Any help would be highly appreciated. Is there any chance that java –version is looking somewhere else than the environment variables?

Please advice!

Thanks in advance!
Goldest

Martin O
November 1st, 2009, 08:33 AM
Even though when I set my path to “C:\j2sdk1.4.2_12\bin”


That should have worked. One possibility is you need to re-start your command prompt after setting the path. JAVA_HOME shouldn't make a difference. Before you type 'java -version' type 'echo %PATH%'. Make sure java doesn't exist in any of the preceding directories to the one you specified.

One thing I find very useful is Cygwin's 'which' command, which you can use to find out exactly which program you're running. It would be nice if the Windows command prompt had this but it doesn't (AFAIK). For example if you did 'which java' you'd probably get something like 'C:\j2sdk1.5\bin\java.exe'.

postmortem
November 1st, 2009, 10:03 PM
there is system and user path... additionally, if both entries are on the path, then whichever is the first one will matter.

ProgramThis
November 2nd, 2009, 08:28 AM
To elaborate on postmortem's post: when you open up the Environment Variables there should be one called CLASSPATH. This one can (and usually does) contain the path to the Java version. Check that there is only one entry leading to any Java JDK.

Then do as dlorde has suggested and go through the entire path argument (you can copy and past it into Notepad to make it more readable). Make sure that the path to 1.5 is before any other ones. Actually I would make sure that it is the ONLY one.

keang
November 2nd, 2009, 09:57 AM
As Martin O suggested 'which' is a useful tool here.

You can download a 'which' for windows (http://gnuwin32.sourceforge.net/packages/which.htm) and use that to find out which Java is actually being run.

On my system I got the following surprising result:
D:\>which java
C:\WINDOWS\system32\java.EXE

goldest
November 3rd, 2009, 04:04 AM
@ Martin O: I have always restarted my command prompt after setting the path. So that can’t be the issue.

@ ProgramThis: I don’t have any environment variable called CLASSPATH, but I have PATH variable where I am providing the JDK path “C:\j2sdk1.4.2_12\bin”. Is this the right way?

@ Postmortem: I would like to know about the system and user path. What I have observed is that whichever version I install second always appears in the “java -version” output at the command prompt. This has happened with both 1.4 and 1.5 installations. Is there anything related to registry or some other settings?

@ Keang: Thanks for the tool. I will surely use it.

Please advice.
Goldest

ProgramThis
November 3rd, 2009, 07:24 AM
@ ProgramThis: I don’t have any environment variable called CLASSPATH, but I have PATH variable where I am providing the JDK path “C:\j2sdk1.4.2_12\bin”. Is this the right way?
Have you made sure that in your PATH that the “C:\j2sdk1.4.2_12\bin” is the FIRST thing in the PATH, and that there are no other links to any other JDKs?

keang
November 3rd, 2009, 09:27 AM
Of course if you want to guarantee you are running a particular version then the safest way is to specify its location at the command prompt ie:
C:\j2sdk1.4.2_12\bin\java MyClassFile

dlorde
November 3rd, 2009, 05:11 PM
@ Martin O: I have always restarted my command prompt after setting the path. So that can’t be the issue.
It will be an issue if you set it from the command prompt - the command prompt gets a copy of the system environment, and all settings made at the command prompt work on that copy - which goes away when you close the command window.

To change the system path permanently, you need to set it in the system environment dialog (from My Computer/Properties/Advanced).

The biggest difference between time and space is that you can't reuse time...
M. Furst

goldest
November 5th, 2009, 03:50 AM
It was in fact the environment variable setting issue. I used the "Which" tool provided by Keang and got the result that the first invoked java.exe was from "C:\WINDOWS\system32\java.exe".

As I was adding my java bin directory at the end of the path, it was not getting into the picture as the first java executable was getting referenced through system32 directory. So whichever installation happened later on was shown at the "java -version" outcome as it was getting updated into system32 directory and ultimately on cmd.

What I learnt: Always specify your path at the beginning in the environment variables and NOT at the end specifically when you have "multiple" java versions installed at your machine.

The right way: .;C:\Program Files\Java\jdk1.5.0_12\bin;%SystemRoot%\system32;

The wrong way: .;%SystemRoot%\system32;C:\Program Files\Java\jdk1.5.0_12\bin

Thanks to all who helped in this.

Goldest