Skip to content

Commit

Permalink
Add extra options when launching Java langserver if JDK's version > 8 (
Browse files Browse the repository at this point in the history
…elastic#36100)

* Add extra options when launching Java langserver if JDK's version > 8

* Rename to needModuleArguments

* Add more comments about extra arguments
  • Loading branch information
Pengcheng Xu authored May 7, 2019
1 parent 8fe1eb2 commit 7711b65
Showing 1 changed file with 36 additions and 21 deletions.
57 changes: 36 additions & 21 deletions x-pack/plugins/code/server/lsp/java_launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { RequestExpander } from './request_expander';

export class JavaLauncher implements ILanguageServerLauncher {
private isRunning: boolean = false;
private needModuleArguments: boolean = true;
constructor(
readonly targetHost: string,
readonly options: ServerOptions,
Expand Down Expand Up @@ -115,8 +116,16 @@ export class JavaLauncher implements ILanguageServerLauncher {

if (this.getSystemJavaHome()) {
const javaHomePath = this.getSystemJavaHome();
if (await this.checkJavaVersion(javaHomePath)) {
const javaVersion = await this.getJavaVersion(javaHomePath);
if (javaVersion > 8) {
// for JDK's versiob > 8, we need extra arguments as default
return javaHomePath;
} else if (javaVersion === 8) {
// JDK's version = 8, needn't extra arguments
this.needModuleArguments = false;
return javaHomePath;
} else {
// JDK's version < 8, use bundled JDK instead, whose version > 8, so need extra arguments as default
}
}

Expand All @@ -142,22 +151,32 @@ export class JavaLauncher implements ILanguageServerLauncher {
process.platform === 'win32' ? 'java.exe' : 'java'
);

let params: string[] = [
'-Declipse.application=org.elastic.jdt.ls.core.id1',
'-Dosgi.bundles.defaultStartLevel=4',
'-Declipse.product=org.elastic.jdt.ls.core.product',
'-Dlog.level=ALL',
'-noverify',
'-Xmx4G',
'-jar',
path.resolve(installationPath, launchersFound[0]),
'-configuration',
this.options.jdtConfigPath,
'-data',
this.options.jdtWorkspacePath,
];

if (this.needModuleArguments) {
params.push('--add-modules=ALL-SYSTEM',
'--add-opens',
'java.base/java.util=ALL-UNNAMED',
'--add-opens',
'java.base/java.lang=ALL-UNNAMED');
}

const p = spawn(
javaPath,
[
'-Declipse.application=org.elastic.jdt.ls.core.id1',
'-Dosgi.bundles.defaultStartLevel=4',
'-Declipse.product=org.elastic.jdt.ls.core.product',
'-Dlog.level=ALL',
'-noverify',
'-Xmx4G',
'-jar',
path.resolve(installationPath, launchersFound[0]),
'-configuration',
this.options.jdtConfigPath,
'-data',
this.options.jdtWorkspacePath,
],
params,
{
detached: false,
stdio: 'pipe',
Expand Down Expand Up @@ -201,19 +220,15 @@ export class JavaLauncher implements ILanguageServerLauncher {
return '';
}

private checkJavaVersion(javaHome: string): Promise<boolean> {
private getJavaVersion(javaHome: string): Promise<number> {
return new Promise((resolve, reject) => {
execFile(
path.resolve(javaHome, 'bin', process.platform === 'win32' ? 'java.exe' : 'java'),
['-version'],
{},
(error, stdout, stderr) => {
const javaVersion = this.parseMajorVersion(stderr);
if (javaVersion < 8) {
resolve(false);
} else {
resolve(true);
}
resolve(javaVersion);
}
);
});
Expand Down

0 comments on commit 7711b65

Please sign in to comment.