This repository was archived by the owner on Mar 16, 2026. It is now read-only.
forked from microsoft/java-debug
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathResolveJavaExecutableHandler.java
More file actions
109 lines (94 loc) · 3.67 KB
/
Copy pathResolveJavaExecutableHandler.java
File metadata and controls
109 lines (94 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*******************************************************************************
* Copyright (c) 2020 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Microsoft Corporation - initial API and implementation
*******************************************************************************/
package com.microsoft.java.debug.plugin.internal;
import java.io.File;
import java.util.List;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.launching.IVMInstall;
import org.eclipse.jdt.launching.JavaRuntime;
import com.microsoft.java.debug.core.Configuration;
public class ResolveJavaExecutableHandler {
private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME);
private static final String[] javaExecCandidates = {
"java",
"java.exe",
"javaw",
"javaw.exe",
"j9",
"j9.exe",
"j9w",
"j9w.exe"
};
private static final String[] javaBinCandidates = {
File.separator,
"bin" + File.separatorChar,
"jre" + File.separatorChar + "bin" + File.separatorChar
};
/**
* Resolve the java executable path from the project's java runtime.
*/
public static String resolveJavaExecutable(List<Object> arguments) throws Exception {
try {
String mainClass = (String) arguments.get(0);
String projectName = (String) arguments.get(1);
IJavaProject targetProject = null;
if (StringUtils.isNotBlank(projectName)) {
targetProject = JdtUtils.getJavaProject(projectName);
} else {
List<IJavaProject> targetProjects = ResolveClasspathsHandler.getJavaProjectFromType(mainClass);
if (!targetProjects.isEmpty()) {
targetProject = targetProjects.get(0);
}
}
return resolveJavaExecutable(targetProject);
} catch (CoreException e) {
logger.log(Level.SEVERE, "Failed to resolve java executable: " + e.getMessage(), e);
}
return null;
}
/**
* Resolve the Java executable path from the project's Java runtime.
*/
public static String resolveJavaExecutable(IJavaProject javaProject) throws CoreException {
if (javaProject == null) {
return null;
}
IVMInstall vmInstall = JavaRuntime.getVMInstall(javaProject);
if (vmInstall == null || vmInstall.getInstallLocation() == null) {
return null;
}
File exe = findJavaExecutable(vmInstall.getInstallLocation());
if (exe == null) {
return null;
}
return exe.getAbsolutePath();
}
private static File findJavaExecutable(File vmInstallLocation) {
boolean isBin = Objects.equals("bin", vmInstallLocation.getName());
for (int i = 0; i < javaExecCandidates.length; i++) {
for (int j = 0; j < javaBinCandidates.length; j++) {
if (!isBin && j == 0) {
continue;
}
File javaFile = new File(vmInstallLocation, javaBinCandidates[j] + javaExecCandidates[i]);
if (javaFile.isFile()) {
return javaFile;
}
}
}
return null;
}
}