Skip to content

Verify JAVA_HOME is set correctly #1587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 9, 2016
Merged
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
57 changes: 46 additions & 11 deletions lib/android-tools-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,30 +156,65 @@ export class AndroidToolsInfo implements IAndroidToolsInfo {
}).future<boolean>()();
}

public validateJavacVersion(installedJavaVersion: string, options?: {showWarningsAsErrors: boolean}): IFuture<boolean> {
public validateJava(javacVersion: string, options?: {showWarningsAsErrors: boolean}): IFuture<boolean> {
return ((): boolean => {
let hasProblemWithJavaVersion = false;
if(options) {
this.showWarningsAsErrors = options.showWarningsAsErrors;
}
let additionalMessage = "You will not be able to build your projects for Android." + EOL

let helpfulMessage = "You will not be able to build your projects for Android." + EOL
+ "To be able to build for Android, verify that you have installed The Java Development Kit (JDK) and configured it according to system requirements as" + EOL +
" described in https://github.com/NativeScript/nativescript-cli#system-requirements.";
let matchingVersion = (installedJavaVersion || "").match(AndroidToolsInfo.VERSION_REGEX);
if(matchingVersion && matchingVersion[1]) {
if(semver.lt(matchingVersion[1], AndroidToolsInfo.MIN_JAVA_VERSION)) {
hasProblemWithJavaVersion = true;
this.printMessage(`Javac version ${installedJavaVersion} is not supported. You have to install at least ${AndroidToolsInfo.MIN_JAVA_VERSION}.`, additionalMessage);

let hasProblemWithJavaHome = this.validateJavaHome(helpfulMessage).wait();
let hasProblemWithJavaVersion: boolean;
if(!hasProblemWithJavaHome) {
hasProblemWithJavaVersion = this.validateJavacVersion(javacVersion, helpfulMessage);
}

return hasProblemWithJavaHome || hasProblemWithJavaVersion;
}).future<boolean>()();
}

private validateJavaHome(helpfulMessage: string): IFuture<boolean> {
return ((): boolean => {
let hasProblemWithJavaHome = false;
let javaHome = process.env.JAVA_HOME;

if(javaHome) {
// validate jarsigner as it does not exist in JRE, but is mandatory for JDK and Android Runtime
let jarSigner = path.join(javaHome, "bin", "jarsigner");
let childProcessResult = this.$childProcess.spawnFromEvent(jarSigner, [], "close", {}, { throwError: false }).wait();
this.$logger.trace(`Result of calling jarsigner from path: ${jarSigner}:`, childProcessResult);
if(childProcessResult.stderr || childProcessResult.exitCode !== 0) {
hasProblemWithJavaHome = true;
this.printMessage("JAVA_HOME environment variable points to incorrect path. Make sure it points to the installation directory of JDK.", helpfulMessage);
}
} else {
hasProblemWithJavaVersion = true;
this.printMessage("Error executing command 'javac'. Make sure you have installed The Java Development Kit (JDK) and set JAVA_HOME environment variable.", additionalMessage);
hasProblemWithJavaHome = true;
this.printMessage("JAVA_HOME environment variable is not set.", helpfulMessage);
}

return hasProblemWithJavaVersion;
return hasProblemWithJavaHome;
}).future<boolean>()();
}

private validateJavacVersion(installedJavaVersion: string, helpfulMessage: string): boolean {
let hasProblemWithJavaVersion = false;
let matchingVersion = (installedJavaVersion || "").match(AndroidToolsInfo.VERSION_REGEX);
if(matchingVersion && matchingVersion[1]) {
if(semver.lt(matchingVersion[1], AndroidToolsInfo.MIN_JAVA_VERSION)) {
hasProblemWithJavaVersion = true;
this.printMessage(`Javac version ${installedJavaVersion} is not supported. You have to install at least ${AndroidToolsInfo.MIN_JAVA_VERSION}.`, helpfulMessage);
}
} else {
hasProblemWithJavaVersion = true;
this.printMessage("Error executing command 'javac'. Make sure you have installed The Java Development Kit (JDK) and set JAVA_HOME environment variable.", helpfulMessage);
}

return hasProblemWithJavaVersion;
}

public getPathToAdbFromAndroidHome(): IFuture<string> {
return (() => {
if(this.androidHome) {
Expand Down
6 changes: 3 additions & 3 deletions lib/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,12 @@ interface IAndroidToolsInfo {
validateInfo(options?: {showWarningsAsErrors: boolean, validateTargetSdk: boolean}): IFuture<boolean>;

/**
* Validates the information about required JAVA version.
* @param {string} installedJavaVersion The JAVA version that will be checked.
* Validates the information about required JAVA version and JAVA_HOME.
* @param {string} javacVersion The JAVA version that will be checked.
* @param {any} options Defines if the warning messages should treated as error.
* @return {boolean} True if there are detected issues, false otherwise.
*/
validateJavacVersion(installedJavaVersion: string, options?: {showWarningsAsErrors: boolean}): IFuture<boolean>;
validateJava(javacVersion: string, options?: {showWarningsAsErrors: boolean}): IFuture<boolean>;

/**
* Returns the path to `android` executable. It should be `$ANDROID_HOME/tools/android`.
Expand Down
2 changes: 1 addition & 1 deletion lib/services/android-project-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject

// this call will fail in case `android` is not set correctly.
this.$androidToolsInfo.getPathToAndroidExecutable({showWarningsAsErrors: true}).wait();
this.$androidToolsInfo.validateJavacVersion(this.$sysInfo.getSysInfo(path.join(__dirname, "..", "..", "package.json")).wait().javacVersion, {showWarningsAsErrors: true}).wait();
this.$androidToolsInfo.validateJava(this.$sysInfo.getSysInfo(path.join(__dirname, "..", "..", "package.json")).wait().javacVersion, {showWarningsAsErrors: true}).wait();
}).future<void>()();
}

Expand Down
2 changes: 1 addition & 1 deletion lib/services/doctor-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class DoctorService implements IDoctorService {
}

let androidToolsIssues = this.$androidToolsInfo.validateInfo().wait();
let javaVersionIssue = this.$androidToolsInfo.validateJavacVersion(sysInfo.javacVersion).wait();
let javaVersionIssue = this.$androidToolsInfo.validateJava(sysInfo.javacVersion).wait();
return result || androidToolsIssues || javaVersionIssue;
}

Expand Down