Check Java Version on Multiple Installed JDKs

  • Context: Java 
  • Thread starter Thread starter rathodnileshn
  • Start date Start date
  • Tags Tags
    Java
Click For Summary

Discussion Overview

The discussion centers around methods to check the version of multiple installed Java Development Kits (JDKs) on a computer. Participants explore various approaches, including command line techniques and Java code, to determine the version of Java being used, while addressing limitations and potential issues with these methods.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant describes using the command prompt to navigate to the JDK path and execute "java -version" to check the version, but notes the inability to redirect the output to a file.
  • Another participant suggests using the command "java -version 2>filename" to capture the output, explaining that the output is sent to stderr instead of stdout.
  • A participant mentions that using System.getProperty("java.lversion") will only return the version of the JVM currently running or the path set in the JAVA_HOME environment variable, not the version of other installed JDKs.
  • There is a discussion about ensuring that the executable in the PATH corresponds to the version specified in JAVA_HOME to avoid confusion with tools like Ant.
  • One participant recalls that in the past, it was preferable to use javaw instead of java on Windows machines, though they express uncertainty about whether this is still true.

Areas of Agreement / Disagreement

Participants generally agree on the limitations of the methods discussed, particularly regarding the output of the "java -version" command and the behavior of System.getProperty. However, there are differing views on the best practices for managing multiple JDK installations and the relevance of using java vs. javaw.

Contextual Notes

Some limitations include the dependency on the environment variable JAVA_HOME and the behavior of different Java executables in relation to the PATH variable. The discussion does not resolve whether the use of javaw is still preferred over java.

rathodnileshn
Messages
2
Reaction score
0
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 can't 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[])
{
try
{
// 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;
System.out.println("<OUTPUT/>");
while((line = br.readLine()) != null )
{ System.out.println(line); } // display process output
System.out.println("</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.
 
Technology news on Phys.org
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":
C:\WINDOWS\system32/java.exe

Then,
>> ... But we can't 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.
 
Hey,

Thank you very much!
You solved my problem!

Thanking you,
Nilesh Rathod.
 
Glad to be of help!
Come and visit us often!
 
rathodnileshn said:
(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.

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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K