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
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -930,9 +930,6 @@
"name": {
"type": "string",
"enum": [
"J2SE-1.5",
"JavaSE-1.6",
"JavaSE-1.7",
"JavaSE-1.8",
"JavaSE-9",
"JavaSE-10",
Expand Down Expand Up @@ -1794,6 +1791,11 @@
"category": "Java",
"title": "Open Java Dashboard",
"icon": "$(dashboard)"
},
{
"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"
}
5 changes: 5 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,11 @@ export namespace Commands {
* Open Java Dashboard
*/
export const OPEN_JAVA_DASHBOARD = 'java.dashboard.open';

/**
* Add Java Runtime
*/
export const ADD_JAVA_RUNTIME = 'java.runtimes.add';
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import { StandardLanguageClient } from './standardLanguageClient';
import { SyntaxLanguageClient } from './syntaxLanguageClient';
import { Telemetry } from './telemetry';
import { cleanJavaLSConfiguration, convertToGlob, deleteClientLog, deleteDirectory, ensureExists, getBuildFilePatterns, getExclusionGlob, getInclusionPatternsFromNegatedExclusion, getJavaConfig, getJavaConfiguration, getVersion, hasBuildToolConflicts, resolveActualCause } from './utils';
import { JavaRuntimes } from './javaRuntimes';

const syntaxClient: SyntaxLanguageClient = new SyntaxLanguageClient();
const standardClient: StandardLanguageClient = new StandardLanguageClient();
Expand Down Expand Up @@ -136,6 +137,7 @@ export async function activate(context: ExtensionContext): Promise<ExtensionAPI>
Dashboard.initialize(context);

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
63 changes: 63 additions & 0 deletions src/javaRuntimes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { getRuntime, IJavaRuntime } from "jdk-utils";
import * as vscode from "vscode";
import { getSupportedJreNames } from "./jdkUtils";
import { Commands } from "./commands";
import * as path from "path";


export namespace JavaRuntimes {
function compatible(runtime: IJavaRuntime, jreName: string): boolean {
if (!runtime.version) {
return true;
}
const majorVersion = runtime.version.major;
if (majorVersion === 8) {
return jreName === 'JavaSE-1.8';
}
const versionStrings = /[0-9]+/g.exec(jreName);
if (versionStrings && versionStrings.length > 0) {
return majorVersion >= parseInt(versionStrings[0]);
}
return false;
}

export async function initialize(context: vscode.ExtensionContext): Promise<void> {
context.subscriptions.push(vscode.commands.registerCommand(Commands.ADD_JAVA_RUNTIME, async () => {
const lastSelectedDirectory: vscode.Uri | undefined = context.workspaceState.get('java.runtimes.lastSelectedDirectory');
const directory = await vscode.window.showOpenDialog({
canSelectFiles: false,
canSelectFolders: true,
canSelectMany: false,
title: 'Select JDK Directory',
defaultUri: lastSelectedDirectory,
});
if (directory) {
context.workspaceState.update('java.runtimes.lastSelectedDirectory', vscode.Uri.file(path.dirname(directory[0].fsPath)));
const runtime = await getRuntime(directory[0].fsPath, {withVersion: true});
if (runtime) {
const config = vscode.workspace.getConfiguration('java.configuration').get('runtimes');
if (Array.isArray(config)) {
const candidates = getSupportedJreNames().filter(name => !config.some(r => r.name === name) && compatible(runtime, name));
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 not prevent overriding existing JRE Mappings, e.g. I want to switch from one JDK 11.0.0 to 11.0.1
  • versions should be sorted by most recent version 1st

if (candidates.length > 0) {
const name = await vscode.window.showQuickPick(candidates, {
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`);
}
} else {
vscode.window.showErrorMessage('No compatible environment available');
}
}
} 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 @@ -29,7 +29,8 @@ suite('Java Language Extension - LightWeight', () => {
Commands.RESTART_LANGUAGE_SERVER,
Commands.FILESEXPLORER_ONPASTE,
Commands.CHANGE_JAVA_SEARCH_SCOPE,
Commands.OPEN_JAVA_DASHBOARD
Commands.OPEN_JAVA_DASHBOARD,
Commands.ADD_JAVA_RUNTIME
].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 @@ -128,7 +128,8 @@ suite('Java Language Extension - Standard', () => {
Commands.FILESEXPLORER_ONPASTE,
Commands.RESOLVE_PASTED_TEXT,
Commands.CHANGE_JAVA_SEARCH_SCOPE,
Commands.OPEN_JAVA_DASHBOARD
Commands.OPEN_JAVA_DASHBOARD,
Commands.ADD_JAVA_RUNTIME
].sort();
const foundJavaCommands = commands.filter((value) => {
return JAVA_COMMANDS.indexOf(value)>=0 || value.startsWith('java.');
Expand Down