您可以通过查看 javac 的安装位置来找到 JDK 路径。假设“javac”在系统的环境路径中,那么您可以通过将“where javac”传递给如下代码来检索路径
public static String getCommandOutput(String command) {
String output = null; //the string to return
Process process = null;
BufferedReader reader = null;
InputStreamReader streamReader = null;
InputStream stream = null;
try {
process = Runtime.getRuntime().exec(command);
//Get stream of the console running the command
stream = process.getInputStream();
streamReader = new InputStreamReader(stream);
reader = new BufferedReader(streamReader);
String currentLine = null; //store current line of output from the cmd
StringBuilder commandOutput = new StringBuilder(); //build up the output from cmd
while ((currentLine = reader.readLine()) != null) {
commandOutput.append(currentLine);
}
int returnCode = process.waitFor();
if (returnCode == 0) {
output = commandOutput.toString();
}
} catch (IOException e) {
System.err.println("Cannot retrieve output of command");
System.err.println(e);
output = null;
} catch (InterruptedException e) {
System.err.println("Cannot retrieve output of command");
System.err.println(e);
} finally {
//Close all inputs / readers
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
System.err.println("Cannot close stream input! " + e);
}
}
if (streamReader != null) {
try {
streamReader.close();
} catch (IOException e) {
System.err.println("Cannot close stream input reader! " + e);
}
}
if (reader != null) {
try {
streamReader.close();
} catch (IOException e) {
System.err.println("Cannot close stream input reader! " + e);
}
}
}
//Return the output from the command - may be null if an error occured
return output;
}
返回的字符串将是 javac 的确切位置,因此您可能需要额外处理才能获取 javac 所在的目录。您需要区分 Windows 和其他操作系统
public static void main(String[] args) {
//"where" on Windows and "whereis" on Linux/Mac
if (System.getProperty("os.name").contains("win") || System.getProperty("os.name").contains("Win")) {
String path = getCommandOutput("where javac");
if (path == null || path.isEmpty()) {
System.err.println("There may have been an error processing the command or ");
System.out.println("JAVAC may not set up to be used from the command line");
System.out.println("Unable to determine the location of the JDK using the command line");
} else {
//Response will be the path including "javac.exe" so need to
//Get the two directories above that
File javacFile = new File(path);
File jdkInstallationDir = javacFile.getParentFile().getParentFile();
System.out.println("jdk in use at command line is: " + jdkInstallationDir.getPath());
}//else: path can be found
} else {
String response = getCommandOutput("whereis javac");
if (response == null) {
System.err.println("There may have been an error processing the command or ");
System.out.println("JAVAC may not set up to be used from the command line");
System.out.println("Unable to determine the location of the JDK using the command line");
} else {
//The response will be "javac: /usr ... "
//so parse from the "/" - if no "/" then there was an error with the command
int pathStartIndex = response.indexOf('/');
if (pathStartIndex == -1) {
System.err.println("There may have been an error processing the command or ");
System.out.println("JAVAC may not set up to be used from the command line");
System.out.println("Unable to determine the location of the JDK using the command line");
} else {
//Else get the directory that is two above the javac.exe file
String path = response.substring(pathStartIndex, response.length());
File javacFile = new File(path);
File jdkInstallationDir = javacFile.getParentFile().getParentFile();
System.out.println("jdk in use at command line is: " + jdkInstallationDir.getPath());
}//else: path found
}//else: response wasn't null
}//else: OS is not windows
}//end main method
注意:如果该方法返回 null / error 这并不意味着 javac 不存在 - 这很可能是 javac 不在 windows 上的 PATH 环境变量中,因此无法使用此方法找到。这在 Unix 上不太可能,因为 Unix 通常会自动将 jdk/bin 目录添加到 PATH 中。此外,这将返回当前在命令行中使用的 javac 版本,不一定是安装的最新版本。因此,如果安装了例如 7u12 和 7u13,但命令提示符设置为使用 7u12,那么这就是将返回的路径。
仅在多台 Windows 机器上进行了测试,但在它们上运行良好。
希望这会有所帮助。