Skip to content
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

[Code]: correctly handle errors in JDK finding #32824

Merged
merged 1 commit into from
Mar 12, 2019
Merged
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
50 changes: 30 additions & 20 deletions x-pack/plugins/code/server/lsp/java_launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,51 +74,61 @@ export class JavaLauncher implements ILanguageServerLauncher {
});
}

private async spawnJava(installationPath: string, port: number, log: Logger) {
const launchersFound = glob.sync('**/plugins/org.eclipse.equinox.launcher_*.jar', {
cwd: installationPath,
});
if (!launchersFound.length) {
log.error('cannot find executable jar for JavaLsp');
}

private async getJavaHome(installationPath: string, log: Logger) {
function findJDK(platform: string) {
const JDKFound = glob.sync(`**/jdks/*${platform}/jdk-*`, {
cwd: installationPath,
});
if (!JDKFound.length) {
log.error('cannot find executable JDK');
log.error('Cannot find Java Home in Bundle installation for ' + platform);
return undefined;
}
return path.resolve(installationPath, JDKFound[0]);
}

let bundledJavaHome = `${findJDK('osx')}/Contents/Home`;
let javaPath = 'java';
let javaHomePath = '';
let bundledJavaHome;

// detect platform
switch (getOsPlatform()) {
const osPlatform = getOsPlatform();
switch (osPlatform) {
case 'darwin':
bundledJavaHome = `${findJDK('osx')}/Contents/Home`;
break;
case 'win32':
bundledJavaHome = `${findJDK('windows')}`;
break;
case 'freebsd':
case 'linux':
bundledJavaHome = `${findJDK('linux')}`;
break;
default:
log.error('Unable to find platform for this os');
log.error('No Bundle JDK defined ' + osPlatform);
}

if (this.getSystemJavaHome()) {
javaHomePath = this.getSystemJavaHome();
if (!(await this.checkJavaVersion(javaHomePath))) {
javaHomePath = '';
const javaHomePath = this.getSystemJavaHome();
if (await this.checkJavaVersion(javaHomePath)) {
return javaHomePath;
}
}
if (javaHomePath === '') {
javaHomePath = bundledJavaHome;

return bundledJavaHome;
}

private async spawnJava(installationPath: string, port: number, log: Logger) {
const launchersFound = glob.sync('**/plugins/org.eclipse.equinox.launcher_*.jar', {
cwd: installationPath,
});
if (!launchersFound.length) {
throw new Error('Cannot find language server jar file');
}

const javaHomePath = await this.getJavaHome(installationPath, log);
if (!javaHomePath) {
throw new Error('Cannot find Java Home');
}
javaPath = path.resolve(

const javaPath = path.resolve(
javaHomePath,
'bin',
process.platform === 'win32' ? 'java.exe' : 'java'
Expand Down