More tweaks to vscode-boot-java jvm finding.

Look for a sibling JDK if the found java exe belongs to a jre.
This commit is contained in:
Kris De Volder
2018-02-05 14:57:53 -08:00
parent 4f180c08c1
commit 476dc27e0b
4 changed files with 53 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
import * as FS from 'fs';
import * as Path from 'path';
import * as ChildProcess from 'child_process';
import { basename } from 'path';
'use strict';
@@ -15,6 +16,11 @@ export interface JVM {
*/
getJavaExecutable() : string
/**
* Path to the corresponding 'java home' for the executable.
*/
getJavaHome() : string
/**
* Detect whether this JVM is a JDK
*/
@@ -46,6 +52,46 @@ export function findJvm(javaHome?: string) : Promise<JVM | null> {
return Promise.resolve(null);
}
/**
* Like findJvm, but additionally, if the found JVM is not a JDK tries to
* find a companion JDK that may be installed alongside it.
*/
export function findJdk(javaHome?: string) : Promise<JVM | null> {
return findJvm(javaHome).then(jvm => {
if (!jvm.isJdk()) {
console.log("found jvm is not a JDK");
//Try to find a 'sibling' JDK.
//Mainly for windows where it is common to have side-by-side install of a jre and jdk, instead of a
//nested jre install inside of a jdk.
//E.g.
//C:\ProgramFiles\Java\jdk1.8.0_161
//C:\ProgramFiles\Java\jre1.8.0_161
let javaExe = jvm.getJavaExecutable();
console.log("javaExe = ", javaExe);
// javaExe example: C:\ProgramFiles\Java\jre1.8.0_161\bin\java.exe
let jhome = jvm.getJavaHome();
console.log("jhome = ", jhome);
let basename : string = Path.basename(jhome);
console.log("basename = ", basename);
let altBasename : string = basename.replace("jre", "jdk");
console.log("altBasename = ", altBasename);
if (altBasename!==basename) {
let altHome = Path.join(Path.dirname(jhome), altBasename);
console.log("altHome = ", altHome);
if (FS.existsSync(altHome)) {
let altExe = Path.resolve(altHome, Path.relative(jhome, javaExe));
console.log("altExe = ", altExe);
return new JavaExecutable(altExe, jvm.getMajorVersion());
}
}
}
return jvm;
});
}
/**
* Find a 'java' exe by looking in the JAVA_HOME and PATH environment variables.
* <p>

View File

@@ -18,7 +18,7 @@ import {WorkspaceEdit, Position} from 'vscode-languageserver-types';
import {HighlightService, HighlightParams} from './highlight-service';
import { log } from 'util';
import { tmpdir } from 'os';
import { JVM, findJvm } from './jvm-util';
import { JVM, findJvm, findJdk } from './jvm-util';
let p2c = P2C.createConverter();
@@ -36,6 +36,7 @@ export interface ActivatorOptions {
jvmHeap?: string;
workspaceOptions?: VSCode.WorkspaceConfiguration;
classpath?: (context: VSCode.ExtensionContext, jvm: JVM) => string[];
preferJdk?: boolean;
}
type JavaOptions = {
@@ -73,7 +74,9 @@ export function activate(options: ActivatorOptions, context: VSCode.ExtensionCon
}
}
return findJvm().then(jvm => {
let findJRE = options.preferJdk ? findJdk : findJvm;
return findJRE().then(jvm => {
if (!jvm) {
VSCode.window.showErrorMessage("Couldn't locate java in $JAVA_HOME or $PATH");
return;

View File

@@ -19,6 +19,7 @@ export function activate(context: VSCode.ExtensionContext) {
DEBUG: false,
CONNECT_TO_LS: false,
extensionId: 'boot-java',
preferJdk: true,
launcher: (context: VSCode.ExtensionContext) => 'org.springframework.boot.loader.JarLauncher',
classpath: (context: VSCode.ExtensionContext, jvm: commons.JVM) => {
const classpath = [

View File

@@ -49,7 +49,7 @@
"vsce-package": "vsce package"
},
"dependencies": {
"commons-vscode": "0.1.4",
"commons-vscode": "file:../commons-vscode/commons-vscode-0.1.4.tgz",
"vscode-languageclient": "^3.4.5"
},
"devDependencies": {