Skip to content

Commit

Permalink
Check if java exists instead of javac. Allow JRE path to be specified…
Browse files Browse the repository at this point in the history
… in xml.java home, java.home, or detect automatically.

Signed-off-by: David Kwon <dakwon@redhat.com>
  • Loading branch information
angelozerr authored and fbricon committed Jun 5, 2019
1 parent a1293cb commit 840fb4e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 46 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ See the [changelog](CHANGELOG.md) for the latest release. You might also find us

## Requirements

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

## Supported VS Code settings

Expand Down
74 changes: 30 additions & 44 deletions src/requirements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const pathExists = require('path-exists');
const expandHomeDir = require('expand-home-dir');
const findJavaHome = require('find-java-home');
const isWindows = process.platform.indexOf('win') === 0;
const JAVAC_FILENAME = 'javac' + (isWindows?'.exe':'');
const JAVA_FILENAME = 'java' + (isWindows?'.exe':'');

export interface RequirementsData {
java_home: string;
Expand Down Expand Up @@ -36,14 +36,37 @@ export async function resolveRequirements(): Promise<RequirementsData> {

function checkJavaRuntime(): Promise<string> {
return new Promise((resolve, reject) => {
let source : string;
let javaHome: string = readXMLJavaHomeConfig();

if (javaHome) {
source = 'The xml.java.home variable defined in VS Code settings';
} else {
javaHome = readJavaHomeConfig();
if (javaHome) {
source = 'The java.home variable defined in VS Code settings';
} else {
javaHome = process.env['JDK_HOME'];
if (javaHome) {
source = 'The JDK_HOME environment variable';
} else {
javaHome = process.env['JAVA_HOME'];
source = 'The JAVA_HOME environment variable';
}
}
}

checkXMLJavaHome(resolve, reject);
checkJavaHome(resolve, reject);
checkEnvVariable('JDK_HOME', resolve, reject);
checkEnvVariable('JAVA_HOME', resolve, reject);

if (javaHome) {
javaHome = expandHomeDir(javaHome);
if (!pathExists.sync(javaHome)) {
openJDKDownload(reject, source+' points to a missing folder');
} else if (!pathExists.sync(path.resolve(javaHome, 'bin', JAVA_FILENAME))){
openJDKDownload(reject, source+ ' does not point to a Java runtime.');
}
return resolve(javaHome);
}
//No settings, let's try to detect as last resort.
findJavaHome(function (err, home) {
findJavaHome({ allowJre: true }, function (err, home) {
if (err){
openJDKDownload(reject, 'Java runtime could not be located.');
}
Expand All @@ -54,31 +77,6 @@ function checkJavaRuntime(): Promise<string> {
});
}

function checkXMLJavaHome(resolve, reject) {
const javaHome = readXMLJavaHomeConfig();
if (!javaHome) {
return;
}
const source = 'The xml.java.home variable defined in VS Code settings';
handleJavaPath(javaHome, source, resolve, reject);
}

function checkJavaHome(resolve, reject) {
const javaHome = readJavaHomeConfig();
if (!javaHome) {
return;
}
const source = 'The java.home variable defined in VS Code settings';
handleJavaPath(javaHome, source, resolve, reject);
}

function checkEnvVariable(name : string, resolve, reject) {
if (!process.env[name]) {
return;
}
const source = `The ${name} environment variable`;
handleJavaPath(process.env[name], source, resolve, reject);
}

function readXMLJavaHomeConfig() : string {
return workspace.getConfiguration('xml').java.home;
Expand All @@ -88,18 +86,6 @@ function readJavaHomeConfig() : string {
const config = workspace.getConfiguration();
return config.get<string>('java.home',null);
}

function handleJavaPath(javaHome : string, source : string, resolve, reject) {
const javaHomeExpanded = expandHomeDir(javaHome);

if (!pathExists.sync(javaHomeExpanded)) {
openJDKDownload(reject, source + ' points to a missing folder.');
}
if (!pathExists.sync(path.resolve(javaHomeExpanded, 'bin', JAVAC_FILENAME))) {
openJDKDownload(reject, source + ' does not point to a JDK.');
}
return resolve(javaHomeExpanded);
}

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

0 comments on commit 840fb4e

Please sign in to comment.