Skip to content

Commit a013912

Browse files
authored
Merge pull request #7822 from sdedic/vscode/default-jdk-check
Attempt to verify that Java exists, and is new enough
2 parents c5731c9 + 936d6ae commit a013912

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

java/java.lsp.server/vscode/src/extension.ts

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import {
4545
import * as net from 'net';
4646
import * as fs from 'fs';
4747
import * as path from 'path';
48-
import { ChildProcess } from 'child_process';
48+
import { spawnSync, ChildProcess } from 'child_process';
4949
import * as vscode from 'vscode';
5050
import * as ls from 'vscode-languageserver-protocol';
5151
import * as launcher from './nbcode';
@@ -73,10 +73,13 @@ const DATABASE: string = 'Database';
7373
const listeners = new Map<string, string[]>();
7474
export let client: Promise<NbLanguageClient>;
7575
export let clientRuntimeJDK : string | null = null;
76+
export const MINIMAL_JDK_VERSION = 17;
77+
7678
let testAdapter: NbTestAdapter | undefined;
7779
let nbProcess : ChildProcess | null = null;
7880
let debugPort: number = -1;
7981
let consoleLog: boolean = !!process.env['ENABLE_CONSOLE_LOG'];
82+
let specifiedJDKWarned : string[] = [];
8083

8184
export class NbLanguageClient extends LanguageClient {
8285
private _treeViewService: TreeViewService;
@@ -465,17 +468,47 @@ export function activate(context: ExtensionContext): VSNetBeansAPI {
465468
// find acceptable JDK and launch the Java part
466469
findJDK(async (specifiedJDK) => {
467470
const osExeSuffix = process.platform === 'win32' ? '.exe' : '';
468-
if (!specifiedJDK || !(fs.existsSync(path.resolve(specifiedJDK, 'bin', `java${osExeSuffix}`)) && path.resolve(specifiedJDK, 'bin', `javac${osExeSuffix}`))) {
471+
let jdkOK : boolean = true;
472+
let javaExecPath : string;
473+
if (!specifiedJDK) {
474+
javaExecPath = 'java';
475+
} else {
476+
javaExecPath = path.resolve(specifiedJDK, 'bin', 'java');
477+
jdkOK = fs.existsSync(path.resolve(specifiedJDK, 'bin', `java${osExeSuffix}`)) && fs.existsSync(path.resolve(specifiedJDK, 'bin', `javac${osExeSuffix}`));
478+
}
479+
if (jdkOK) {
480+
log.appendLine(`Verifying java: ${javaExecPath}`);
481+
// let the shell interpret PATH and .exe extension
482+
let javaCheck = spawnSync(`${javaExecPath} -version`, { shell : true });
483+
if (javaCheck.error || javaCheck.status) {
484+
jdkOK = false;
485+
} else {
486+
javaCheck.stderr.toString().split('\n').find(l => {
487+
// yes, versions like 1.8 (up to 9) will be interpreted as 1, which is OK for < comparison
488+
let re = /.* version \"([0-9]+)\.[^"]+\".*/.exec(l);
489+
if (re) {
490+
let versionNumber = Number(re[1]);
491+
if (versionNumber < MINIMAL_JDK_VERSION) {
492+
jdkOK = false;
493+
}
494+
}
495+
});
496+
}
497+
}
498+
let warnedJDKs : string[] = specifiedJDKWarned;
499+
if (!jdkOK && !warnedJDKs.includes(specifiedJDK || '')) {
469500
const msg = specifiedJDK ?
470-
`The current path to JDK "${specifiedJDK}" may be invalid. A valid JDK is required by Apache NetBeans Language Server to run.
471-
You should configure a proper JDK for Apache NetBeans and/or other technologies. Do you want to run JDK configuration now ?` :
472-
'A valid JDK is required by Apache NetBeans Language Server to run, but none was found. You should configure a proper JDK for Apache NetBeans and/or other technologies. ' +
473-
'Do you want to run JDK configuration now ?';
501+
`The current path to JDK "${specifiedJDK}" may be invalid. A valid JDK ${MINIMAL_JDK_VERSION}+ is required by Apache NetBeans Language Server to run.
502+
You should configure a proper JDK for Apache NetBeans and/or other technologies. Do you want to run JDK configuration now?` :
503+
`A valid JDK ${MINIMAL_JDK_VERSION}+ is required by Apache NetBeans Language Server to run, but none was found. You should configure a proper JDK for Apache NetBeans and/or other technologies. ` +
504+
'Do you want to run JDK configuration now?';
474505
const Y = "Yes";
475506
const N = "No";
476507
if (await vscode.window.showErrorMessage(msg, Y, N) == Y) {
477508
vscode.commands.executeCommand('nbls.jdk.configuration');
478509
return;
510+
} else {
511+
warnedJDKs.push(specifiedJDK || '');
479512
}
480513
}
481514
let currentClusters = findClusters(context.extensionPath).sort();

java/java.lsp.server/vscode/src/jdk/settings.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import * as vscode from 'vscode';
2121
import * as path from 'path';
2222
import * as process from 'process';
2323
import * as jdk from './jdk';
24+
import { MINIMAL_JDK_VERSION } from '../extension';
2425

2526

2627
export abstract class Setting {
@@ -341,7 +342,7 @@ const NBLS_EXTENSION_ID = 'asf.apache-netbeans-java';
341342
const NBLS_SETTINGS_NAME = 'Language Server by Apache NetBeans';
342343
const NBLS_SETTINGS_PROPERTY = 'netbeans.jdkhome';
343344
function nblsSetting(): Setting {
344-
return new JavaSetting(NBLS_SETTINGS_NAME, NBLS_SETTINGS_PROPERTY, 17, true);
345+
return new JavaSetting(NBLS_SETTINGS_NAME, NBLS_SETTINGS_PROPERTY, MINIMAL_JDK_VERSION, true);
345346
}
346347

347348
const NBLS_SETTINGS_PROJECT_NAME = 'Language Server by Apache NetBeans - Java Runtime for Projects';

0 commit comments

Comments
 (0)