有没有人知道如何使用Java编程方式获取已安装的所有JVM(不包括默认的)?
例如,用户计算机上安装了2个JVM:
JDK 5
JDK 6
我需要知道所有已安装的版本,以便切换正在使用的版本(默认情况下),然后调用javac程序来使用特定的JDK版本编译一些源代码。
我在网上找了一些信息,发现:
- 如何通过编程方式获取Java版本(而不是默认版本)? - 如何通过编程方式获取jdk / javac的路径? 但我没有找到我需要的内容。
有没有人知道如何使用Java编程方式获取已安装的所有JVM(不包括默认的)?
例如,用户计算机上安装了2个JVM:
JDK 5
JDK 6
我最近处理了一个非常相似的情况。以下代码几乎完全符合您的需求。它寻找Java JRE和JDK,不仅限于JDK,但应该很容易根据您的需求进行编辑。注意:仅适用于Windows。
/**
* Java Finder by petrucio@stackoverflow(828681) is licensed under a Creative Commons Attribution 3.0 Unported License.
* Needs WinRegistry.java. Get it at: https://dev59.com/InVD5IYBdhLWcg3wKoaH
*
* JavaFinder - Windows-specific classes to search for all installed versions of java on this system
* Author: petrucio@stackoverflow (828681)
*****************************************************************************/
import java.util.*;
import java.io.*;
/**
* Helper class to fetch the stdout and stderr outputs from started Runtime execs
* Modified from http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=4
*****************************************************************************/
class RuntimeStreamer extends Thread {
InputStream is;
String lines;
RuntimeStreamer(InputStream is) {
this.is = is;
this.lines = "";
}
public String contents() {
return this.lines;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ( (line = br.readLine()) != null) {
this.lines += line + "\n";
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/**
* Execute a command and wait for it to finish
* @return The resulting stdout and stderr outputs concatenated
****************************************************************************/
public static String execute(String[] cmdArray) {
try {
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(cmdArray);
RuntimeStreamer outputStreamer = new RuntimeStreamer(proc.getInputStream());
RuntimeStreamer errorStreamer = new RuntimeStreamer(proc.getErrorStream());
outputStreamer.start();
errorStreamer.start();
proc.waitFor();
return outputStreamer.contents() + errorStreamer.contents();
} catch (Throwable t) {
t.printStackTrace();
}
return null;
}
public static String execute(String cmd) {
String[] cmdArray = { cmd };
return RuntimeStreamer.execute(cmdArray);
}
}
/**
* Helper struct to hold information about one installed java version
****************************************************************************/
class JavaInfo {
public String path; //! Full path to java.exe executable file
public String version; //! Version string. "Unkown" if the java process returned non-standard version string
public boolean is64bits; //! true for 64-bit javas, false for 32
/**
* Calls 'javaPath -version' and parses the results
* @param javaPath: path to a java.exe executable
****************************************************************************/
public JavaInfo(String javaPath) {
String versionInfo = RuntimeStreamer.execute( new String[] { javaPath, "-version" } );
String[] tokens = versionInfo.split("\"");
if (tokens.length < 2) this.version = "Unkown";
else this.version = tokens[1];
this.is64bits = versionInfo.toUpperCase().contains("64-BIT");
this.path = javaPath;
}
/**
* @return Human-readable contents of this JavaInfo instance
****************************************************************************/
public String toString() {
return this.path + ":\n Version: " + this.version + "\n Bitness: " + (this.is64bits ? "64-bits" : "32-bits");
}
}
/**
* Windows-specific java versions finder
*****************************************************************************/
public class JavaFinder {
/**
* @return: A list of javaExec paths found under this registry key (rooted at HKEY_LOCAL_MACHINE)
* @param wow64 0 for standard registry access (32-bits for 32-bit app, 64-bits for 64-bits app)
* or WinRegistry.KEY_WOW64_32KEY to force access to 32-bit registry view,
* or WinRegistry.KEY_WOW64_64KEY to force access to 64-bit registry view
* @param previous: Insert all entries from this list at the beggining of the results
*************************************************************************/
private static List<String> searchRegistry(String key, int wow64, List<String> previous) {
List<String> result = previous;
try {
List<String> entries = WinRegistry.readStringSubKeys(WinRegistry.HKEY_LOCAL_MACHINE, key, wow64);
for (int i = 0; entries != null && i < entries.size(); i++) {
String val = WinRegistry.readString(WinRegistry.HKEY_LOCAL_MACHINE, key + "\\" + entries.get(i), "JavaHome", wow64);
if (!result.contains(val + "\\bin\\java.exe")) {
result.add(val + "\\bin\\java.exe");
}
}
} catch (Throwable t) {
t.printStackTrace();
}
return result;
}
/**
* @return: A list of JavaInfo with informations about all javas installed on this machine
* Searches and returns results in this order:
* HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment (32-bits view)
* HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment (64-bits view)
* HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit (32-bits view)
* HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit (64-bits view)
* WINDIR\system32
* WINDIR\SysWOW64
****************************************************************************/
public static List<JavaInfo> findJavas() {
List<String> javaExecs = new ArrayList<String>();
javaExecs = JavaFinder.searchRegistry("SOFTWARE\\JavaSoft\\Java Runtime Environment", WinRegistry.KEY_WOW64_32KEY, javaExecs);
javaExecs = JavaFinder.searchRegistry("SOFTWARE\\JavaSoft\\Java Runtime Environment", WinRegistry.KEY_WOW64_64KEY, javaExecs);
javaExecs = JavaFinder.searchRegistry("SOFTWARE\\JavaSoft\\Java Development Kit", WinRegistry.KEY_WOW64_32KEY, javaExecs);
javaExecs = JavaFinder.searchRegistry("SOFTWARE\\JavaSoft\\Java Development Kit", WinRegistry.KEY_WOW64_64KEY, javaExecs);
javaExecs.add(System.getenv("WINDIR") + "\\system32\\java.exe");
javaExecs.add(System.getenv("WINDIR") + "\\SysWOW64\\java.exe");
List<JavaInfo> result = new ArrayList<JavaInfo>();
for (String javaPath: javaExecs) {
if (!(new File(javaPath).exists())) continue;
result.add(new JavaInfo(javaPath));
}
return result;
}
/**
* @return: The path to a java.exe that has the same bitness as the OS
* (or null if no matching java is found)
****************************************************************************/
public static String getOSBitnessJava() {
String arch = System.getenv("PROCESSOR_ARCHITECTURE");
String wow64Arch = System.getenv("PROCESSOR_ARCHITEW6432");
boolean isOS64 = arch.endsWith("64") || (wow64Arch != null && wow64Arch.endsWith("64"));
List<JavaInfo> javas = JavaFinder.findJavas();
for (int i = 0; i < javas.size(); i++) {
if (javas.get(i).is64bits == isOS64) return javas.get(i).path;
}
return null;
}
/**
* Standalone testing - lists all Javas in the system
****************************************************************************/
public static void main(String [] args) {
List<JavaInfo> javas = JavaFinder.findJavas();
for (int i = 0; i < javas.size(); i++) {
System.out.println("\n" + javas.get(i));
}
}
}
>java JavaFinder
C:\Program Files (x86)\Java\jre6\bin\java.exe:
Version: 1.6.0_31
Bitness: 32-bits
C:\Program Files\Java\jre6\bin\java.exe:
Version: 1.6.0_31
Bitness: 64-bits
D:\Dev\Java\jdk1.6.0_31\bin\java.exe:
Version: 1.6.0_31
Bitness: 64-bits
C:\Windows\system32\java.exe:
Version: 1.6.0_31
Bitness: 64-bits
C:\Windows\SysWOW64\java.exe:
Version: 1.6.0_31
Bitness: 32-bits
java -version:1.6 -version
如果您已经安装了Java,该命令将返回“java版本”1.6.0_xx“”。如果您未安装它,则会显示“无法找到符合1.6规范的JRE”。
在Linux上似乎不起作用,可能是因为Linux没有标准的安装Java的方式。
wmic product where "name LIKE '%%java%%'" get * /format:textvaluelist > temp_javavers.txt
notepad.exe temp_javavers.txt
installed这个词有点含糊不清 - 即使JRE和JDK随着一个安装程序一起提供,实际上我们只需要将JRE或JDK的文件复制到机器上,就可以立即使用它。
因此,通常您需要查找本地计算机上所有的java
或java.exe
可执行文件,并调用java -version
来查看1,这个名为java
/java.exe
的可执行文件是否真的是(部分)JRE。
1这种方法存在“不冒险,不好玩”的风险,请仔细查看Sean的评论!如果您不能信任“机器”(或其“用户”),则可能需要测试可执行文件是脚本/批处理还是二进制文件 - 即使二进制文件也可能擦除您的磁盘,即使原始的javac可执行文件也可能被一些恶意代码替换...
我不确定这是否回答了你的问题,但是你可以使用Family Versioning功能控制小程序或WebStart应用程序将运行在哪个主要JRE版本上。这允许您指定小程序运行的主要Java版本。你应该能够从那里推导出javac
的位置。
http://www.oracle.com/technetwork/java/javase/family-clsid-140615.html
deployJava.js
也可用于编写JWS应用程序的启动按钮。 - Andrew Thompson-source
、-target
和-bootclasspath
设置适当的选项。后两个选项是javac
的交叉编译选项之一。JFileChooser
,将当前JRE路径作为默认目录。如果用户无法从那里导航到JDK,则他们可能不应该编写代码。要查找已安装的Java版本,比寻找javac.exe更好的方法是检查注册表。Java会创建一个名为HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environment
的注册表键,其中包含一个字符串CurrentVersion
,其值为版本号,例如1.5或1.6。
您可以使用该信息来查找JVM:
您可以在此处查看更多信息:http://java.sun.com/j2se/1.4.2/runtime_win32.html
我不熟悉如何从Java中访问注册表,但您始终可以运行regedit的命令行界面并解析结果,这就是我所做的。
如先前所述,HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment
应列出可用的 JVM,但我刚刚发现,尽管此注册表目前仅在我的计算机上列出了一个 64 位的 1.6.0_31 JVM,但我还安装了一个 32 位的 java 在 C:\Windows\SysWOW64
下。
因此,注册表中列出的信息并不能完全反映实际情况,如果您想在 64 位 Windows 上运行 32 位 JVM,则还应尝试使用 C:\Windows\SysWOW64
。