Skip to content

Commit d60dd80

Browse files
committed
feat: java home set
1 parent b144c6c commit d60dd80

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

bundled/scripts/noConfigScripts/README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,15 @@ debugjava -jar myapp.jar --spring.profiles.active=dev
6262
## How It Works Internally
6363

6464
1. When you run `debugjava`, the wrapper script temporarily sets `JAVA_TOOL_OPTIONS=-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=0`
65-
2. The wrapper launches the Java process with JDWP enabled
66-
3. JVM starts and outputs: "Listening for transport dt_socket at address: 12345"
67-
4. The wrapper captures the JDWP port from this output
68-
5. The port is written to a communication file
69-
6. VS Code's file watcher detects the file and automatically starts an attach debug session
65+
2. The wrapper determines which Java executable to use (priority order):
66+
- First: `JAVA_HOME/bin/java` if JAVA_HOME environment variable is set (user's explicit choice)
67+
- Second: `VSCODE_JAVA_EXEC` environment variable (Java path from VS Code's Java Language Server)
68+
- Third: `java` command from system PATH
69+
3. The wrapper launches the Java process with JDWP enabled
70+
4. JVM starts and outputs: "Listening for transport dt_socket at address: 12345"
71+
5. The wrapper captures the JDWP port from this output
72+
6. The port is written to a communication file
73+
7. VS Code's file watcher detects the file and automatically starts an attach debug session
7074

7175
## Troubleshooting
7276

bundled/scripts/noConfigScripts/jdwp-wrapper.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ const isDebugEnabled = javaToolOptions.includes('jdwp') && endpointFile;
2323

2424
// Helper function to find java command
2525
function getJavaCommand() {
26+
// Priority 1: Try JAVA_HOME environment variable first (user's explicit choice)
2627
const javaHome = process.env.JAVA_HOME;
27-
28-
// Try JAVA_HOME first
2928
if (javaHome) {
3029
const javaPath = path.join(javaHome, 'bin', 'java');
3130
const javaPathExe = process.platform === 'win32' ? `${javaPath}.exe` : javaPath;
@@ -38,10 +37,16 @@ function getJavaCommand() {
3837
return javaPath;
3938
}
4039

41-
console.warn(`[Java Debug] JAVA_HOME is set to '${javaHome}', but java command not found there. Falling back to PATH.`);
40+
console.warn(`[Java Debug] JAVA_HOME is set to '${javaHome}', but java command not found there. Falling back to VS Code's Java.`);
41+
}
42+
43+
// Priority 2: Use VSCODE_JAVA_EXEC if provided by VS Code (from Java Language Server)
44+
const vscodeJavaExec = process.env.VSCODE_JAVA_EXEC;
45+
if (vscodeJavaExec && fs.existsSync(vscodeJavaExec)) {
46+
return vscodeJavaExec;
4247
}
4348

44-
// Fall back to 'java' in PATH
49+
// Priority 3: Fall back to 'java' in PATH
4550
return 'java';
4651
}
4752

src/noConfigDebugInit.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as crypto from 'crypto';
77
import * as vscode from 'vscode';
88

99
import { sendInfo, sendError } from "vscode-extension-telemetry-wrapper";
10+
import { getJavaHome } from "./utility";
1011

1112
/**
1213
* Registers the configuration-less debugging setup for the extension.
@@ -76,6 +77,19 @@ export async function registerNoConfigDebug(
7677
// only in the debugjava wrapper scripts (debugjava.ps1, debugjava.bat, debugjava)
7778
collection.replace('VSCODE_JDWP_ADAPTER_ENDPOINTS', tempFilePath);
7879

80+
// Try to get Java executable from Java Language Server
81+
// This ensures we use the same Java version as the project is compiled with
82+
try {
83+
const javaHome = await getJavaHome();
84+
if (javaHome) {
85+
const javaExec = path.join(javaHome, 'bin', 'java');
86+
collection.replace('VSCODE_JAVA_EXEC', javaExec);
87+
}
88+
} catch (error) {
89+
// If we can't get Java from Language Server, that's okay
90+
// The wrapper script will fall back to JAVA_HOME or PATH
91+
}
92+
7993
const noConfigScriptsDir = path.join(extPath, 'bundled', 'scripts', 'noConfigScripts');
8094
const pathSeparator = process.platform === 'win32' ? ';' : ':';
8195

0 commit comments

Comments
 (0)