1

I found, we can find out Java version using System.getProperty("java.version"). from here - Getting Java version at runtime

But I have already done so coding using Runtime.getRuntime().exec(commands) -

String[] commands ={"java", "-version"};
    String line;
    String cmdOutput = "";
    try {
        Process process = Runtime.getRuntime().exec(commands);
        process.waitFor();
        BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
        while ((line = input.readLine()) != null) {
            cmdOutput += (line + "\n");
        }
        System.out.println("output "+cmdOutput);
        input.close();
        }
    catch (Exception ex) {
        ex.printStackTrace();
    }

But getting blank output.

When I run java -version from command prompt, we get version, I feel it should also return same output.

Before I discard it and use System.getProperty("java.version"), Can I please know what I am missing here ?

Thanks in advance.

Community
  • 1
  • 1
Ninad Pingale
  • 6,801
  • 5
  • 32
  • 55
  • Have you checked the error stream? – Andrew Stubbs Jul 18 '14 at 09:46
  • @ Andrew Stubbs: Thanks it worked using errorstream. But why did it go in errorstream ? – Ninad Pingale Jul 18 '14 at 09:49
  • 1
    Don't use `Runtime.exec` to get the java version, unless you intend to never give the application to anyone else or to ever run it on a different machine. First, the JRE may or may not in the `PATH` system variable. Second, even if it is, there is no guarantee that your application will be run with the same JRE as the one present in the `PATH` – blgt Jul 18 '14 at 10:02

2 Answers2

1

You don't get the version in your code because: java -version prints to the error stream, rather than stdout, I don't know why.

You can show this with:

java -version > output.txt

and see that it's still printed to your console, and nothing is in output.txt.

Or with:

java -version 2> error.txt

and see that nothing is printed and the version information is in error.txt

The question as to why it happens was asked here: Why does 'java -version' go to stderr?

Community
  • 1
  • 1
Andrew Stubbs
  • 4,322
  • 3
  • 29
  • 48
0
public static String getJavaVersion()
  {
    String method = "getJavaVersion";
    String javaVersion = "";
    try
    {
      // Command:  wmic datafile where name="C:\\Windows\\System32\\msiexec.exe" get Version /value
      Process process = Runtime.getRuntime().exec("java -version");
      BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
      javaVersion = reader.readLine();

      System.out.println("Method: "+method+" -   "+javaVersion);
      while(javaVersion!= null)
      {
        if(javaVersion.trim().startsWith("Version"))
        {
          System.out.println("Method: "+method+" -   "+javaVersion);
          return javaVersion;
        }

        javaVersion=reader.readLine();
      }// end while
    }
    catch (IOException e)
    {
      System.out.println("Method: "+method+"  Could not check version "+e);
      e.printStackTrace();
      return "";
    }
    return javaVersion;
  }
Adam Kipnis
  • 10,175
  • 10
  • 35
  • 48
  • While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-??code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Adam Kipnis Apr 15 '18 at 18:51