CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Oct 2009
    Posts
    8

    Question "java -version" doesn't display proper value.

    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

  2. #2
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: "java -version" doesn't display proper value.

    Quote Originally Posted by goldest View Post
    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'.

  3. #3
    Join Date
    Oct 2008
    Posts
    77

    Re: "java -version" doesn't display proper value.

    there is system and user path... additionally, if both entries are on the path, then whichever is the first one will matter.

  4. #4
    Join Date
    Feb 2008
    Posts
    966

    Re: "java -version" doesn't display proper value.

    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.

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: "java -version" doesn't display proper value.

    As Martin O suggested 'which' is a useful tool here.

    You can download a 'which' for windows 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

  6. #6
    Join Date
    Oct 2009
    Posts
    8

    Question Re: "java -version" doesn't display proper value.

    @ 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

  7. #7
    Join Date
    Feb 2008
    Posts
    966

    Re: "java -version" doesn't display proper value.

    Quote Originally Posted by goldest View Post
    @ 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?

  8. #8
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: "java -version" doesn't display proper value.

    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:
    Code:
    C:\j2sdk1.4.2_12\bin\java MyClassFile

  9. #9
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: "java -version" doesn't display proper value.

    Quote Originally Posted by goldest View Post
    @ 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
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  10. #10
    Join Date
    Oct 2009
    Posts
    8

    Thumbs up Re: "java -version" doesn't display proper value.

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured