Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Check java version

  1. Apr 2, 2009 #1
    Hi all,

    Suppose on computer 4 different versions of java are installed.
    (1) Depending on the path of java.exe we can open command prompt go to path /jdk_version/bin/java.exe and type "java -version" and know the
    particular version, But we cant extract the output of the command "java -version" into a file.

    (2) Also using System.getProperty("java.lversion"); in java code we can get java version but it will return the version of the jvm that is running that
    program. Or the path set to environment variable JAVA_HOME.
    (3) Also following code can be used but it's not working:!!!(don't know why??)
    import java.io.*;
    import java.lang.*;

    public class x

    public static void main (String args[])
    // get runtime environment and execute child process
    Runtime systemShell = Runtime.getRuntime();
    Process output = systemShell.exec("java -version");
    // open reader to get output from process
    BufferedReader br = new BufferedReader (new InputStreamReader(output.getInputStream()));
    String line = null;
    while((line = br.readLine()) != null )
    { System.out.println(line); } // display process output
    int exitVal = output.waitFor(); // get process exit value
    System.out.println("Process Exit Value : "+ exitVal);
    catch (IOException ioe){ System.err.println(ioe); }
    catch (Throwable t) { t.printStackTrace();}
    *** My requirement is :***

    4 or more jdk are installed on OS. depending on path of java.exe I need to find it's version.

    Can anybody help please?????

    Thanks in advance,
    Nilesh Rathod.
  2. jcsd
  3. Apr 17, 2009 #2
    First, I believe the copy of java that is executed is in c:\windows\system32\java.exe, which forms part of the JRE. You can verify this for your computer using a utility called "which" that is a translation to Windows of the same command in Unix.
    This is what I get when I type "which java":

    >> ... But we cant extract the output of the command "java -version" into a file.
    Assuming all you need to know is to find out which version of Java your current JRE is using, you can capture the output of
    java -version
    by the command:
    java -version 2>filename

    The 2 is required before the redirection operator > because java.exe directs its output to stderr and not stdout.

    example output on my computer:
    java version "1.6.0_05"
    Java(TM) SE Runtime Environment (build 1.6.0_05-b13)
    Java HotSpot(TM) Client VM (build 10.0-b19, mixed mode, sharing)

    Hope that sovles your problem.
  4. Apr 23, 2009 #3

    Thank you very much!!!!
    You solved my problem!!!!!!

    Thanking you,
    Nilesh Rathod.
  5. Apr 23, 2009 #4
    Glad to be of help!
    Come and visit us often!
  6. Apr 25, 2009 #5
    System.getProperty will return the version of the JVM that is running, not the value of the JAVA_HOME environment variable.

    It is possible to get the value of JAVA_HOME, if you really want that, but not through that call. Also, mathmate's advice about using which is excellent. Finally, it's usually desirable to make sure that the executable pointed to in your PATH corresponds to the version specified in JAVA_HOME, or else tools like Ant may get confused.

    I'm not 100% sure that this is still true, but in the olden days, you should prefer to run javaw on a windows machine, and not java.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook