java.exe的安装位置,如何找到javaw.exe的安装位置?

So, for a project I am working on, I need to find out where a javaw.exe is located on a user's machine. How do I do that? Assuming that user is on Windows machine

The method that I used is limited to English versions of Windows only.

I looked for where the OS is installed, locate the Program Files directory, locate Java, jdk directory, bin, then javaw.exe. I know this will not work on non-English versions of Windows.

What is the human-language-independent way to do this ?

解决方案

For the sake of completeness, let me mention that there are some places (on a Windows PC) to look for javaw.exe in case it is not found in the path:

(Still Reimeus' suggestion should be your first attempt.)

1.

Java usually stores it's location in Registry, under the following key:

HKLM\Software\JavaSoft\Java Runtime Environement\\JavaHome

2.

Newer versions of JRE/JDK, seem to also place a copy of javaw.exe in 'C:\Windows\System32', so one might want to check there too (although chances are, if it is there, it will be found in the path as well).

3.

Of course there are the "usual" install locations:

'C:\Program Files\Java\jre*\bin'

'C:\Program Files\Java\jdk*\bin'

'C:\Program Files (x86)\Java\jre*\bin'

'C:\Program Files (x86)\Java\jdk*\bin'

[Note, that for older versions of Windows (XP, Vista(?)), this will only help on english versions of the OS. Fortunately, on later version of Windows "Program Files" will point to the directory regardless of its "display name" (which is language-specific).]

A little while back, I wrote this piece of code to check for javaw.exe in the aforementioned places. Maybe someone finds it useful:

static protected String findJavaw() {

Path pathToJavaw = null;

Path temp;

/* Check in Registry: HKLM\Software\JavaSoft\Java Runtime Environement\\JavaHome */

String keyNode = "HKLM\\Software\\JavaSoft\\Java Runtime Environment";

List output = new ArrayList<>();

executeCommand(new String[] {"REG", "QUERY", "\"" + keyNode + "\"",

"/v", "CurrentVersion"},

output);

Pattern pattern = Pattern.compile("\\s*CurrentVersion\\s+\\S+\\s+(.*)$");

for (String line : output) {

Matcher matcher = pattern.matcher(line);

if (matcher.find()) {

keyNode += "\\" + matcher.group(1);

List output2 = new ArrayList<>();

executeCommand(

new String[] {"REG", "QUERY", "\"" + keyNode + "\"",

"/v", "JavaHome"},

output2);

Pattern pattern2

= Pattern.compile("\\s*JavaHome\\s+\\S+\\s+(.*)$");

for (String line2 : output2) {

Matcher matcher2 = pattern2.matcher(line2);

if (matcher2.find()) {

pathToJavaw = Paths.get(matcher2.group(1), "bin",

"javaw.exe");

break;

}

}

break;

}

}

try {

if (Files.exists(pathToJavaw)) {

return pathToJavaw.toString();

}

} catch (Exception ignored) {}

/* Check in 'C:\Windows\System32' */

pathToJavaw = Paths.get("C:\\Windows\\System32\\javaw.exe");

try {

if (Files.exists(pathToJavaw)) {

return pathToJavaw.toString();

}

} catch (Exception ignored) {}

/* Check in 'C:\Program Files\Java\jre*' */

pathToJavaw = null;

temp = Paths.get("C:\\Program Files\\Java");

if (Files.exists(temp)) {

try (DirectoryStream dirStream

= Files.newDirectoryStream(temp, "jre*")) {

for (Path path : dirStream) {

temp = Paths.get(path.toString(), "bin", "javaw.exe");

if (Files.exists(temp)) {

pathToJavaw = temp;

// Don't "break", in order to find the latest JRE version

}

}

if (pathToJavaw != null) {

return pathToJavaw.toString();

}

} catch (Exception ignored) {}

}

/* Check in 'C:\Program Files (x86)\Java\jre*' */

pathToJavaw = null;

temp = Paths.get("C:\\Program Files (x86)\\Java");

if (Files.exists(temp)) {

try (DirectoryStream dirStream

= Files.newDirectoryStream(temp, "jre*")) {

for (Path path : dirStream) {

temp = Paths.get(path.toString(), "bin", "javaw.exe");

if (Files.exists(temp)) {

pathToJavaw = temp;

// Don't "break", in order to find the latest JRE version

}

}

if (pathToJavaw != null) {

return pathToJavaw.toString();

}

} catch (Exception ignored) {}

}

/* Check in 'C:\Program Files\Java\jdk*' */

pathToJavaw = null;

temp = Paths.get("C:\\Program Files\\Java");

if (Files.exists(temp)) {

try (DirectoryStream dirStream

= Files.newDirectoryStream(temp, "jdk*")) {

for (Path path : dirStream) {

temp = Paths.get(path.toString(), "jre", "bin", "javaw.exe");

if (Files.exists(temp)) {

pathToJavaw = temp;

// Don't "break", in order to find the latest JDK version

}

}

if (pathToJavaw != null) {

return pathToJavaw.toString();

}

} catch (Exception ignored) {}

}

/* Check in 'C:\Program Files (x86)\Java\jdk*' */

pathToJavaw = null;

temp = Paths.get("C:\\Program Files (x86)\\Java");

if (Files.exists(temp)) {

try (DirectoryStream dirStream

= Files.newDirectoryStream(temp, "jdk*")) {

for (Path path : dirStream) {

temp = Paths.get(path.toString(), "jre", "bin", "javaw.exe");

if (Files.exists(temp)) {

pathToJavaw = temp;

// Don't "break", in order to find the latest JDK version

}

}

if (pathToJavaw != null) {

return pathToJavaw.toString();

}

} catch (Exception ignored) {}

}

return "javaw.exe"; // Let's just hope it is in the path :)

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值