@@ -10,6 +10,7 @@ const expandHomeDir = require("expand-home-dir");
1010const WinReg = require ( "winreg-utf8" ) ;
1111const isWindows : boolean = process . platform . indexOf ( "win" ) === 0 ;
1212const isMac : boolean = process . platform . indexOf ( "darwin" ) === 0 ;
13+ const isLinux : boolean = process . platform . indexOf ( "linux" ) === 0 ;
1314const JAVAC_FILENAME = "javac" + ( isWindows ? ".exe" : "" ) ;
1415const JAVA_FILENAME = "java" + ( isWindows ? ".exe" : "" ) ;
1516
@@ -26,8 +27,9 @@ export async function findJavaHomes(): Promise<JavaRuntime[]> {
2627 const ret : JavaRuntime [ ] = [ ] ;
2728 const jdkMap = new Map < string , string [ ] > ( ) ;
2829
29- updateJDKs ( jdkMap , await fromJavaHomeEnv ( ) , "env.JAVA_HOME" ) ;
30- updateJDKs ( jdkMap , await fromPathEnv ( ) , "env.PATH" ) ;
30+ updateJDKs ( jdkMap , await fromEnv ( "JDK_HOME" ) , "env.JDK_HOME" ) ;
31+ updateJDKs ( jdkMap , await fromEnv ( "JAVA_HOME" ) , "env.JAVA_HOME" ) ;
32+ updateJDKs ( jdkMap , await fromPath ( ) , "env.PATH" ) ;
3133 updateJDKs ( jdkMap , await fromWindowsRegistry ( ) , "WindowsRegistry" ) ;
3234 updateJDKs ( jdkMap , await fromCommonPlaces ( ) , "DefaultLocation" ) ;
3335
@@ -59,18 +61,19 @@ function updateJDKs(map: Map<string, string[]>, newJdks: string[], source: strin
5961 }
6062}
6163
62- async function fromJavaHomeEnv ( ) : Promise < string [ ] > {
64+ async function fromEnv ( name : string ) : Promise < string [ ] > {
6365 const ret : string [ ] = [ ] ;
64- if ( process . env . JAVA_HOME ) {
65- const javaHome = await verifyJavaHome ( process . env . JAVA_HOME , JAVAC_FILENAME ) ;
66+ const proposed = process . env [ name ] ;
67+ if ( proposed ) {
68+ const javaHome = await verifyJavaHome ( proposed , JAVAC_FILENAME ) ;
6669 if ( javaHome ) {
6770 ret . push ( javaHome ) ;
6871 }
6972 }
7073 return ret ;
7174}
7275
73- async function fromPathEnv ( ) : Promise < string [ ] > {
76+ async function fromPath ( ) : Promise < string [ ] > {
7477 const ret : string [ ] = [ ] ;
7578
7679 const paths = process . env . PATH ? process . env . PATH . split ( path . delimiter ) . filter ( Boolean ) : [ ] ;
@@ -235,25 +238,39 @@ async function fromCommonPlaces(): Promise<string[]> {
235238 }
236239 }
237240
241+ // common place for Linux
242+ if ( isLinux ) {
243+ const jvmStore = "/usr/lib/jvm" ;
244+ let jvms : string [ ] = [ ] ;
245+ try {
246+ jvms = await fse . readdir ( jvmStore ) ;
247+ } catch ( error ) {
248+ // ignore
249+ }
250+ for ( const jvm of jvms ) {
251+ const proposed = path . join ( jvmStore , jvm ) ;
252+ const javaHome = await verifyJavaHome ( proposed , JAVAC_FILENAME ) ;
253+ if ( javaHome ) {
254+ ret . push ( javaHome ) ;
255+ }
256+ }
257+ }
258+
238259 return ret ;
239260}
240261
241-
242-
243- async function verifyJavaHome ( raw : string , javaFilename : string ) : Promise < string | undefined > {
262+ export async function verifyJavaHome ( raw : string , javaFilename : string ) : Promise < string | undefined > {
244263 const dir = expandHomeDir ( raw ) ;
245264 const targetJavaFile = await findLinkedFile ( path . resolve ( dir , "bin" , javaFilename ) ) ;
246265 const proposed = path . dirname ( path . dirname ( targetJavaFile ) ) ;
247266 if ( await fse . pathExists ( proposed )
248- && ( await fse . lstat ( proposed ) ) . isDirectory ( )
249267 && await fse . pathExists ( path . resolve ( proposed , "bin" , javaFilename ) )
250268 ) {
251269 return proposed ;
252270 }
253271 return undefined ;
254272}
255273
256-
257274// iterate through symbolic links until file is found
258275async function findLinkedFile ( file : string ) : Promise < string > {
259276 if ( ! await fse . pathExists ( file ) || ! ( await fse . lstat ( file ) ) . isSymbolicLink ( ) ) {
@@ -262,8 +279,7 @@ async function findLinkedFile(file: string): Promise<string> {
262279 return await findLinkedFile ( await fse . readlink ( file ) ) ;
263280}
264281
265-
266- export async function getJavaVersion ( javaHome : string ) : Promise < number | undefined > {
282+ export async function getJavaVersion ( javaHome : string ) : Promise < number > {
267283 let javaVersion = await checkVersionInReleaseFile ( javaHome ) ;
268284 if ( ! javaVersion ) {
269285 javaVersion = await checkVersionByCLI ( javaHome ) ;
@@ -319,7 +335,10 @@ async function checkVersionInReleaseFile(javaHome: string): Promise<number> {
319335/**
320336 * Get version by parsing `JAVA_HOME/bin/java -version`
321337 */
322- function checkVersionByCLI ( javaHome : string ) : Promise < number > {
338+ async function checkVersionByCLI ( javaHome : string ) : Promise < number > {
339+ if ( ! javaHome ) {
340+ return 0 ;
341+ }
323342 return new Promise ( ( resolve , reject ) => {
324343 const javaBin = path . join ( javaHome , "bin" , JAVA_FILENAME ) ;
325344 cp . execFile ( javaBin , [ "-version" ] , { } , ( error , stdout , stderr ) => {
0 commit comments