Skip to content

Commit 840fb4e

Browse files
angelozerrfbricon
authored andcommitted
Check if java exists instead of javac. Allow JRE path to be specified in xml.java home, java.home, or detect automatically.
Signed-off-by: David Kwon <dakwon@redhat.com>
1 parent a1293cb commit 840fb4e

File tree

2 files changed

+33
-46
lines changed

2 files changed

+33
-46
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ See the [changelog](CHANGELOG.md) for the latest release. You might also find us
3636

3737
## Requirements
3838

39-
* Java JDK 8 or more recent
39+
* Java JDK (or JRE) 8 or more recent
4040
* Ensure Java path is set in either:
4141
* `xml.java.home` in VSCode preferences
4242
* `java.home` in VSCode preferences
4343
* Environment variable `JAVA_HOME` or `JDK_HOME`
4444
* **Note**: The path should end at the parent folder that contains the `bin` folder.
45-
**Example Path**: `/usr/lib/jvm/java-1.8.0` if `bin` exists at `/usr/lib/jvm/java-1.8.0/bin`.
45+
**Example Path**: `/usr/lib/jvm/java-1.8.0` if `bin` exists at `/usr/lib/jvm/java-1.8.0/bin`.
46+
* **Note**: If the path is not set, the extension will attempt to find the path to the JDK or JRE.
4647

4748
## Supported VS Code settings
4849

src/requirements.ts

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const pathExists = require('path-exists');
88
const expandHomeDir = require('expand-home-dir');
99
const findJavaHome = require('find-java-home');
1010
const isWindows = process.platform.indexOf('win') === 0;
11-
const JAVAC_FILENAME = 'javac' + (isWindows?'.exe':'');
11+
const JAVA_FILENAME = 'java' + (isWindows?'.exe':'');
1212

1313
export interface RequirementsData {
1414
java_home: string;
@@ -36,14 +36,37 @@ export async function resolveRequirements(): Promise<RequirementsData> {
3636

3737
function checkJavaRuntime(): Promise<string> {
3838
return new Promise((resolve, reject) => {
39+
let source : string;
40+
let javaHome: string = readXMLJavaHomeConfig();
41+
42+
if (javaHome) {
43+
source = 'The xml.java.home variable defined in VS Code settings';
44+
} else {
45+
javaHome = readJavaHomeConfig();
46+
if (javaHome) {
47+
source = 'The java.home variable defined in VS Code settings';
48+
} else {
49+
javaHome = process.env['JDK_HOME'];
50+
if (javaHome) {
51+
source = 'The JDK_HOME environment variable';
52+
} else {
53+
javaHome = process.env['JAVA_HOME'];
54+
source = 'The JAVA_HOME environment variable';
55+
}
56+
}
57+
}
3958

40-
checkXMLJavaHome(resolve, reject);
41-
checkJavaHome(resolve, reject);
42-
checkEnvVariable('JDK_HOME', resolve, reject);
43-
checkEnvVariable('JAVA_HOME', resolve, reject);
44-
59+
if (javaHome) {
60+
javaHome = expandHomeDir(javaHome);
61+
if (!pathExists.sync(javaHome)) {
62+
openJDKDownload(reject, source+' points to a missing folder');
63+
} else if (!pathExists.sync(path.resolve(javaHome, 'bin', JAVA_FILENAME))){
64+
openJDKDownload(reject, source+ ' does not point to a Java runtime.');
65+
}
66+
return resolve(javaHome);
67+
}
4568
//No settings, let's try to detect as last resort.
46-
findJavaHome(function (err, home) {
69+
findJavaHome({ allowJre: true }, function (err, home) {
4770
if (err){
4871
openJDKDownload(reject, 'Java runtime could not be located.');
4972
}
@@ -54,31 +77,6 @@ function checkJavaRuntime(): Promise<string> {
5477
});
5578
}
5679

57-
function checkXMLJavaHome(resolve, reject) {
58-
const javaHome = readXMLJavaHomeConfig();
59-
if (!javaHome) {
60-
return;
61-
}
62-
const source = 'The xml.java.home variable defined in VS Code settings';
63-
handleJavaPath(javaHome, source, resolve, reject);
64-
}
65-
66-
function checkJavaHome(resolve, reject) {
67-
const javaHome = readJavaHomeConfig();
68-
if (!javaHome) {
69-
return;
70-
}
71-
const source = 'The java.home variable defined in VS Code settings';
72-
handleJavaPath(javaHome, source, resolve, reject);
73-
}
74-
75-
function checkEnvVariable(name : string, resolve, reject) {
76-
if (!process.env[name]) {
77-
return;
78-
}
79-
const source = `The ${name} environment variable`;
80-
handleJavaPath(process.env[name], source, resolve, reject);
81-
}
8280

8381
function readXMLJavaHomeConfig() : string {
8482
return workspace.getConfiguration('xml').java.home;
@@ -88,18 +86,6 @@ function readJavaHomeConfig() : string {
8886
const config = workspace.getConfiguration();
8987
return config.get<string>('java.home',null);
9088
}
91-
92-
function handleJavaPath(javaHome : string, source : string, resolve, reject) {
93-
const javaHomeExpanded = expandHomeDir(javaHome);
94-
95-
if (!pathExists.sync(javaHomeExpanded)) {
96-
openJDKDownload(reject, source + ' points to a missing folder.');
97-
}
98-
if (!pathExists.sync(path.resolve(javaHomeExpanded, 'bin', JAVAC_FILENAME))) {
99-
openJDKDownload(reject, source + ' does not point to a JDK.');
100-
}
101-
return resolve(javaHomeExpanded);
102-
}
10389

10490
function checkJavaVersion(java_home: string): Promise<number> {
10591
return new Promise((resolve, reject) => {

0 commit comments

Comments
 (0)