2

Our software has a swing panel that's used to list jdk installation paths. For example, if there are 4 jdk installed in user's PC and the jdk paths are listed in the panel:

  1. C:\Java\jdk1.5.0_19\bin\java.exe

  2. D:\software\Java6\jdk1.6.0_31\bin\java.exe

  3. D:\software\Java6\jdk1.6.0_31_64\bin\java.exe

  4. D:\installedapp\jdk1.7.0_03\bin\java.exe

The user needs to pick one of jdk installation paths to install our software. We want to know the bit version (32bit or 64bit) of jdk picked by user, how can we do that?

Kevin Bedell
  • 13,254
  • 10
  • 78
  • 114
Jemyxu
  • 33
  • 1
  • 1
  • 4
  • What have you tried? And you need to at least give us the code for the panel -- what component do you store your paths in? JLabels? – Roddy of the Frozen Peas Jun 28 '12 at 02:02
  • You can imitate the situation like that, I give a jdk installation path(2.D:\software\Java6\jdk1.6.0_31\bin\java.exe or 3.D:\software\Java6\jdk1.6.0_31_64\bin\java.exe) to you, how can you know the jdk is 32bit or 64bit ? – Jemyxu Jun 28 '12 at 02:10
  • Yes ... but how are they placed on the panel? JLabels, JButtons, AWT classes or what? – Roddy of the Frozen Peas Jun 28 '12 at 02:11
  • We have a method to find all jdk installed in user's PC, then store the path on a JLabel, also, we have some JRadioButton for user to select. – Jemyxu Jun 28 '12 at 02:29

3 Answers3

3

well, if you know the location, you could probably use Runtime.exec("pathToJavaInstallPath/bin/java.exe -version")and capture the version that way. That's at least one brute force way.

Sample output:

java version "1.6.0_32"
Java(TM) SE Runtime Environment (build 1.6.0_32-b05)
Java HotSpot(TM) 64-Bit Server VM (build 20.7-b02, mixed mode)
ZhaoGang
  • 4,491
  • 1
  • 27
  • 39
Matt
  • 11,523
  • 2
  • 23
  • 33
  • Thanks, I tried it, but it outputs nothing. String j6 = "D:/software/Java6/jdk1.6.0_31_64/bin/java.exe -version"; Process process = Runtime.getRuntime().exec(j6); InputStream in = process.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String str = null; while ((str = br.readLine()) != null) { System.out.println(str); } br.close(); – Jemyxu Jun 28 '12 at 03:03
  • 2
    I found the solution finally, "java -version" writes to the error stream, not the output stream. Therefore, I should use getErrorStream() instead of getInputStream(). We can refer to the link below. [problem with Runtime getRuntime() exec(command)](http://www.coderanch.com/t/385943/java/java/Runtime-getRuntime-exec-command). Thanks a lot, especially Matt, Kevin. – Jemyxu Jun 28 '12 at 06:12
  • 1
    Addition: If the JDK version is 32 bit, then instead of 64-bit server VM, Client VM will be displayed in the 3rd line. – RAS Oct 16 '15 at 09:01
0

You can also use GetBinaryType Win32 API function to determine whether the given .exe is 32 or 64 bit.

See question How to detect that a given PE file (exe or dll) is 64 bit or 32 bit for more details.

Community
  • 1
  • 1
Alexey Ivanov
  • 11,541
  • 4
  • 39
  • 68
-1

You can read the version of Java that's executing using the "java.version" property:

You can also read the architecture (e.g. x86 vs amd64), OS and JRE home.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • I'm afraid that using "java.version" property can just display the default jdk but not the jdk selected by user, you set jdk5 in system path, but you select jdk7 in the panel, then System.getProperty("java.version") is the version of jdk, also, the version is not the bit verion. – Jemyxu Jun 28 '12 at 02:22
  • or `java.specification.version` gives the major release, e.g. 1.6 or 1.7 – vikingsteve Nov 05 '13 at 08:09