feat(run): run and debug Java programs when jdtls does not serve the module (#4240)

With jdtls disabled and another Java language server (java-analyzer) serving the module, Run and Test failed with "no Java language server (jdtls) is configured". Launches of such modules now build with Maven or Gradle and take the classpath and JDK from the server's java-analyzer/projectClasspath request, falling back to Maven's resolved classpath; JDK selection, command-line shortening and Lombok hints are shared with the jdtls path, which is unchanged. Debugging those modules uses java-analyzer's own debug adapter (`java-analyzer dap`). Java no longer colours JDK symbols (defaultLibrary) differently from library and workspace ones.
This commit is contained in:
l0ng-ai
2026-11-19 04:26:58 +08:00
committed by GitHub
parent 431cfafab3
commit c57a52348b
18 changed files with 492 additions and 38 deletions
+36 -1
View File
@@ -2,7 +2,8 @@
* Java debugging through Microsoft java-debug, a jdtls bundle: jdtls starts its
* DAP server (`vscode.java.startDebugSession`) and the session connects to that
* port (`debugServer`). Application configurations are launched by java-debug;
* JVMs started by Maven / Gradle with a JDWP agent are attached to.
* JVMs started by Maven / Gradle with a JDWP agent are attached to. Modules that
* jdtls doesn't serve debug with java-analyzer's own adapter (`java-analyzer dap`).
*/
import { executeAction } from "@/platform/actions";
import { call, commands, IpcError, nextRequestId, type LaunchConfig, type RunConfig } from "@/platform/ipc";
@@ -21,6 +22,7 @@ import { withEnvFiles } from "./launch";
export const JAVA_ADAPTER = "java-debug";
export const KOTLIN_ADAPTER = "kotlin-debug-adapter";
export const JAVA_ANALYZER_ADAPTER = "java-analyzer";
const KOTLIN_LSP = "kotlin-lsp";
export function isJavaDebuggable(rc: RunConfig): boolean {
@@ -191,6 +193,27 @@ export async function javaLaunchConfig(
}),
};
}
if ((await call(commands.javaDebugAdapter(module))) === JAVA_ANALYZER_ADAPTER) {
preparation?.signal.throwIfAborted();
return {
name: rc.name,
adapter: JAVA_ANALYZER_ADAPTER,
request: "launch",
program: null,
args: [],
cwd,
env: { ...rc.env },
extraJson: JSON.stringify({
projectRoot: join(root, module),
mainClass: parsed.mainClass,
classPaths,
modulePaths: info.modulePaths,
javaExec: resolved.command === "java" ? undefined : resolved.command,
vmArgs: [...consoleVmArgs(rc), ...parsed.vmArgs],
args: parsed.programArgs,
}),
};
}
const port = await debugServer(module, preparation?.signal);
preparation?.signal.throwIfAborted();
// java-debug resolves the Java project for Evaluate on its own from `mainClass` only when
@@ -252,6 +275,18 @@ export async function javaAttachConfig(target: JavaAttachTarget, root: string):
extraJson: JSON.stringify({ projectRoot: cwd, hostName: target.hostName, port: target.port, timeout: 30_000 }),
};
}
if ((await call(commands.javaDebugAdapter(target.module))) === JAVA_ANALYZER_ADAPTER) {
return {
name: target.name,
adapter: JAVA_ANALYZER_ADAPTER,
request: "attach",
program: null,
args: [],
cwd,
env: {},
extraJson: JSON.stringify({ projectRoot: cwd, hostName: target.hostName, port: target.port, timeout: 30_000 }),
};
}
const port = await debugServer(target.module);
// java-debug resolves the Java project for Evaluate from `mainClass` only when the class is in
// a project it imported; a JDWP attach cannot even see the main class, so name the project the