Skip to content

Commit 6bcccea

Browse files
committed
Marginally improve failed Java home detection
- display detected path - check if java home ends with `bin` - opens settings.json if java.home preference is invalid Signed-off-by: Fred Bricon <[email protected]>
1 parent 1e84b35 commit 6bcccea

File tree

5 files changed

+67
-36
lines changed

5 files changed

+67
-36
lines changed

package-lock.json

Lines changed: 9 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/commands.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,9 @@ export namespace Commands {
109109
/**
110110
* Generate hashCode() and equals().
111111
*/
112-
export const HASHCODE_EQUALS_PROMPT = 'java.action.hashCodeEqualsPrompt';
112+
export const HASHCODE_EQUALS_PROMPT = 'java.action.hashCodeEqualsPrompt';
113+
/**
114+
* Open settings.json
115+
*/
116+
export const OPEN_JSON_SETTINGS = 'workbench.action.openSettingsJson';
113117
}

src/extension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
3030
return requirements.resolveRequirements().catch(error => {
3131
//show error
3232
window.showErrorMessage(error.message, error.label).then((selection) => {
33-
if (error.label && error.label === selection && error.openUrl) {
34-
commands.executeCommand(Commands.OPEN_BROWSER, error.openUrl);
33+
if (error.label && error.label === selection && error.command) {
34+
commands.executeCommand(error.command, error.commandParam);
3535
}
3636
});
3737
// rethrow to disrupt the chain.

src/requirements.ts

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import * as path from 'path';
66
import * as pathExists from 'path-exists';
77
import * as expandHomeDir from 'expand-home-dir';
88
import * as findJavaHome from 'find-java-home';
9+
import { Commands } from './commands';
910

1011
const isWindows = process.platform.indexOf('win') === 0;
11-
const JAVAC_FILENAME = 'javac' + (isWindows?'.exe':'');
12+
const JAVAC_FILENAME = 'javac' + (isWindows ? '.exe' : '');
1213

1314
export interface RequirementsData {
1415
java_home: string;
@@ -18,8 +19,8 @@ export interface RequirementsData {
1819
interface ErrorData {
1920
message: string;
2021
label: string;
21-
openUrl: Uri;
22-
replaceClose: boolean;
22+
command: string;
23+
commandParam: any;
2324
}
2425
/**
2526
* Resolves the requirements needed to run the extension.
@@ -31,65 +32,71 @@ interface ErrorData {
3132
export async function resolveRequirements(): Promise<RequirementsData> {
3233
let java_home = await checkJavaRuntime();
3334
let javaVersion = await checkJavaVersion(java_home);
34-
return Promise.resolve({ 'java_home': java_home, 'java_version': javaVersion});
35+
return Promise.resolve({ 'java_home': java_home, 'java_version': javaVersion });
3536
}
3637

3738
function checkJavaRuntime(): Promise<string> {
3839
return new Promise((resolve, reject) => {
39-
let source : string;
40-
let javaHome : string = readJavaConfig();
40+
let source: string;
41+
let javaHome: string = readJavaConfig();
4142
if (javaHome) {
42-
source = 'The java.home variable defined in VS Code settings';
43+
source = 'java.home variable defined in VS Code settings';
4344
} else {
4445
javaHome = process.env['JDK_HOME'];
4546
if (javaHome) {
46-
source = 'The JDK_HOME environment variable';
47+
source = 'JDK_HOME environment variable';
4748
} else {
4849
javaHome = process.env['JAVA_HOME'];
49-
source = 'The JAVA_HOME environment variable';
50+
source = 'JAVA_HOME environment variable';
5051
}
5152
}
52-
if(javaHome ){
53+
if (javaHome) {
5354
javaHome = expandHomeDir(javaHome);
54-
if(!pathExists.sync(javaHome)){
55-
openJDKDownload(reject, source+' points to a missing folder');
55+
if (!pathExists.sync(javaHome)) {
56+
invalidJavaHome(reject, `The ${source} points to a missing or inaccessible folder (${javaHome})`);
5657
}
57-
if(!pathExists.sync(path.resolve(javaHome, 'bin', JAVAC_FILENAME))){
58-
openJDKDownload(reject, source+ ' does not point to a JDK.');
58+
else if (!pathExists.sync(path.resolve(javaHome, 'bin', JAVAC_FILENAME))) {
59+
let msg: string;
60+
if (pathExists.sync(path.resolve(javaHome, JAVAC_FILENAME))) {
61+
msg = `'bin' should be removed from the ${source} (${javaHome})`;
62+
} else {
63+
msg = `The ${source} (${javaHome}) does not point to a JDK.`
64+
}
65+
invalidJavaHome(reject, msg);
5966
}
6067
return resolve(javaHome);
6168
}
6269
//No settings, let's try to detect as last resort.
6370
findJavaHome(function (err, home) {
64-
if (err){
65-
openJDKDownload(reject,'Java runtime could not be located');
66-
}
67-
else {
68-
resolve(home);
69-
}
70-
});
71+
if (err) {
72+
openJDKDownload(reject, 'Java runtime (JDK, not JRE) could not be located');
73+
}
74+
else {
75+
resolve(home);
76+
}
77+
});
7178
});
7279
}
7380

74-
function readJavaConfig() : string {
81+
function readJavaConfig(): string {
7582
const config = workspace.getConfiguration();
76-
return config.get<string>('java.home',null);
83+
return config.get<string>('java.home', null);
7784
}
7885

7986
function checkJavaVersion(java_home: string): Promise<number> {
8087
return new Promise((resolve, reject) => {
8188
cp.execFile(java_home + '/bin/java', ['-version'], {}, (error, stdout, stderr) => {
8289
let javaVersion = parseMajorVersion(stderr);
83-
if (javaVersion < 8){
90+
if (javaVersion < 8) {
8491
openJDKDownload(reject, 'Java 8 or more recent is required to run. Please download and install a recent JDK');
85-
} else{
92+
} else {
8693
resolve(javaVersion);
8794
}
8895
});
8996
});
9097
}
9198

92-
export function parseMajorVersion(content:string):number {
99+
export function parseMajorVersion(content: string): number {
93100
let regexp = /version "(.*)"/g;
94101
let match = regexp.exec(content);
95102
if (!match) {
@@ -119,7 +126,21 @@ function openJDKDownload(reject, cause) {
119126
reject({
120127
message: cause,
121128
label: 'Get the Java Development Kit',
122-
openUrl: Uri.parse(jdkUrl),
123-
replaceClose: false
129+
command: Commands.OPEN_BROWSER,
130+
commandParam: Uri.parse(jdkUrl),
124131
});
125132
}
133+
134+
function invalidJavaHome(reject, cause: string) {
135+
if (cause.indexOf("java.home") > -1) {
136+
reject({
137+
message: cause,
138+
label: 'Open settings',
139+
command: Commands.OPEN_JSON_SETTINGS
140+
});
141+
} else {
142+
reject({
143+
message: cause,
144+
});
145+
}
146+
}

test/extension.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ suite('Java Language Extension', () => {
4242
Commands.REMOVE_FROM_SOURCEPATH,
4343
Commands.LIST_SOURCEPATHS,
4444
Commands.OVERRIDE_METHODS_PROMPT,
45-
Commands.HASHCODE_EQUALS_PROMPT
45+
Commands.HASHCODE_EQUALS_PROMPT,
46+
Commands.OPEN_JSON_SETTINGS
4647
];
4748
let foundJavaCommands = commands.filter(function(value){
4849
return JAVA_COMMANDS.indexOf(value)>=0 || value.startsWith('java.');

0 commit comments

Comments
 (0)