Check java version

  • #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[])
{
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.
 

Answers and Replies

  • #2
365
0
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 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.
 
  • #3
Hey,

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

Thanking you,
Nilesh Rathod.
 
  • #4
365
0
Glad to be of help!
Come and visit us often!
 
  • #5
(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.
 

Related Threads on Check java version

  • Last Post
Replies
1
Views
2K
Replies
3
Views
14K
Replies
5
Views
1K
Replies
2
Views
10K
  • Last Post
Replies
2
Views
2K
Replies
1
Views
592
  • Last Post
Replies
9
Views
2K
  • Last Post
Replies
2
Views
1K
  • Last Post
Replies
4
Views
2K
Top