Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1752,6 +1752,11 @@
"command": "java.action.showExtendedOutline",
"title": "%java.action.showExtendedOutline%",
"category": "Java"
},
{
"command": "java.runtimes.add",
"title": "%java.runtimes.add%",
"category": "Java"
}
],
"keybindings": [
Expand Down
3 changes: 2 additions & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
"java.action.filesExplorerPasteAction": "Paste Clipboard Text Into a File",
"java.action.doCleanup": "Performs Cleanup Actions",
"java.change.searchScope": "Change Search Scope",
"java.action.showExtendedOutline": "Open Extended Outline"
"java.action.showExtendedOutline": "Open Extended Outline",
"java.runtimes.add": "Add Java Runtime"
}
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { BuildFileSelector, PICKED_BUILD_FILES, cleanupWorkspaceState } from './
import { pasteFile } from './pasteAction';
import { ServerStatusKind } from './serverStatus';
import { TelemetryService } from '@redhat-developer/vscode-redhat-telemetry/lib/node';
import { JavaRuntimes } from './javaRuntimes';

const syntaxClient: SyntaxLanguageClient = new SyntaxLanguageClient();
const standardClient: StandardLanguageClient = new StandardLanguageClient();
Expand Down Expand Up @@ -114,6 +115,7 @@ export function fixJdtLinksInDocumentation(oldDocumentation: MarkdownString): Ma

export async function activate(context: ExtensionContext): Promise<ExtensionAPI> {
await loadSupportedJreNames(context);
await JavaRuntimes.initialize(context);
context.subscriptions.push(commands.registerCommand(Commands.FILESEXPLORER_ONPASTE, async () => {
const originalClipboard = await env.clipboard.readText();
// Hack in order to get path to selected folder if applicable (see https://github.com/microsoft/vscode/issues/3553#issuecomment-1098562676)
Expand Down
42 changes: 42 additions & 0 deletions src/javaRuntimes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { getRuntime } from "jdk-utils";
import * as vscode from "vscode";
import { getSupportedJreNames } from "./jdkUtils";


export namespace JavaRuntimes {
export async function initialize(context: vscode.ExtensionContext): Promise<void> {
context.subscriptions.push(vscode.commands.registerCommand('java.runtimes.add', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably add "java.runtimes.add" to Commands.

const directory = await vscode.window.showOpenDialog({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe default to parent folder of last selected JDK (just to make my testing easier)

canSelectFiles: false,
canSelectFolders: true,
canSelectMany: false,
title: 'Select JDK Directory',
});
if (directory) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth validating the directory by at least checking ${directory}/bin/javac exists to ensure it's probably a valid JDK ?

const runtime = await getRuntime(directory[0].fsPath);
if (runtime) {
const config = vscode.workspace.getConfiguration('java.configuration').get('runtimes');
if (Array.isArray(config)) {
if (config.some(r => r.path === directory[0].fsPath)) {
vscode.window.showErrorMessage(`JDK Directory ${directory[0].fsPath} already configured`);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No you need to ensure uniqueness of the jre name.

Screenshot 2025-10-09 at 10 09 08

It's OK having the same JDK used for 2 runtimes, eg. use JDK 11 for JavaSE-11 and JavaSE-1.8

} else {
const name = await vscode.window.showQuickPick(getSupportedJreNames(), {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would improve it by:

  • detecting the java version of the selected path
  • preselect the matching execution environment name
  • filter out incompatible environments, i.e don't show JavaSE-22 and above if you selected a JDK 21.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also pre Java 8 support has been dropped over a year ago. Can you remove the J2SE-1.5 ... JavaSE-7 values in package.json

title: 'Select Java Runtime',
});
if (name) {
config.push({
name: name,
path: directory[0].fsPath,
});
}
vscode.workspace.getConfiguration('java.configuration').update('runtimes', config, vscode.ConfigurationTarget.Global);
vscode.window.showInformationMessage(`JDK Directory ${directory[0].fsPath} added`);
Comment on lines +32 to +33
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be executed only if the quickpick was complete. If I press ESC while the wizard is open, I see the "...added" message.

}
}
} else {
vscode.window.showErrorMessage(`Invalid JDK Directory ${directory[0].fsPath}`);
}
}
}));
}
}
3 changes: 2 additions & 1 deletion test/lightweight-mode-suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ suite('Java Language Extension - LightWeight', () => {
Commands.CLEAN_SHARED_INDEXES,
Commands.RESTART_LANGUAGE_SERVER,
Commands.FILESEXPLORER_ONPASTE,
Commands.CHANGE_JAVA_SEARCH_SCOPE
Commands.CHANGE_JAVA_SEARCH_SCOPE,
"java.runtimes.add"
].sort();
const foundJavaCommands = commands.filter((value) => {
return JAVA_COMMANDS.indexOf(value)>=0 || value.startsWith('java.');
Expand Down
3 changes: 2 additions & 1 deletion test/standard-mode-suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ suite('Java Language Extension - Standard', () => {
Commands.RESOLVE_SOURCE_ATTACHMENT,
Commands.FILESEXPLORER_ONPASTE,
Commands.RESOLVE_PASTED_TEXT,
Commands.CHANGE_JAVA_SEARCH_SCOPE
Commands.CHANGE_JAVA_SEARCH_SCOPE,
"java.runtimes.add"
].sort();
const foundJavaCommands = commands.filter((value) => {
return JAVA_COMMANDS.indexOf(value)>=0 || value.startsWith('java.');
Expand Down