@@ -22,6 +22,9 @@ export class SysInfo implements NativeScriptDoctor.ISysInfo {
2222 private monoVerRegExp = / v e r s i o n ( \d + [ . ] \d + [ . ] \d + ) / gm;
2323
2424 private javaCompilerVerCache : string ;
25+ private javaVerCache : string ;
26+ private javaVerJavaHomeCache : string ;
27+ private javaVerPathCache : string ;
2528 private xCodeVerCache : string ;
2629 private npmVerCache : string ;
2730 private nodeVerCache : string ;
@@ -62,6 +65,25 @@ export class SysInfo implements NativeScriptDoctor.ISysInfo {
6265 } ) ;
6366 }
6467
68+ public getJavaVersion ( ) : Promise < string > {
69+ return this . getValueForProperty ( ( ) => this . javaVerCache , async ( ) : Promise < string > => {
70+ const javacVersion = ( await this . getJavaVersionFromJavaHome ( ) ) || ( await this . getJavaVersionFromPath ( ) ) ;
71+ return javacVersion ;
72+ } ) ;
73+ }
74+
75+ public getJavaVersionFromPath ( ) : Promise < string > {
76+ return this . getValueForProperty ( ( ) => this . javaVerPathCache , ( ) : Promise < string > => {
77+ return this . getVersionOfJavaExecutableFromPath ( Constants . JAVA_EXECUTABLE_NAME ) ;
78+ } ) ;
79+ }
80+
81+ public getJavaVersionFromJavaHome ( ) : Promise < string > {
82+ return this . getValueForProperty ( ( ) => this . javaVerJavaHomeCache , ( ) : Promise < string > => {
83+ return this . getVersionOfJavaExecutableFromJavaHome ( Constants . JAVA_EXECUTABLE_NAME ) ;
84+ } ) ;
85+ }
86+
6587 public getXcodeVersion ( ) : Promise < string > {
6688 return this . getValueForProperty ( ( ) => this . xCodeVerCache , async ( ) : Promise < string > => {
6789 if ( this . hostInfo . isDarwin ) {
@@ -494,39 +516,47 @@ export class SysInfo implements NativeScriptDoctor.ISysInfo {
494516 }
495517
496518 private async getJavacVersionFromPath ( ) : Promise < string > {
497- let javacVersion : string = null ;
498-
499- try {
500- const detectionCommand = this . hostInfo . isWindows ? "where" : "which" ;
501- // if this command succeeds, we have javac in the PATH. In case it is not there, it will throw an error.
502- await this . childProcess . exec ( `${ detectionCommand } ${ Constants . JAVAC_EXECUTABLE_NAME } ` ) ;
503- javacVersion = await this . executeJavacVersion ( Constants . JAVAC_EXECUTABLE_NAME ) ;
504- } catch ( err ) { /* intentionally left blank */ }
519+ return this . getVersionOfJavaExecutableFromPath ( Constants . JAVAC_EXECUTABLE_NAME ) ;
520+ }
505521
506- return javacVersion ;
522+ private getJavacVersionFromJavaHome ( ) : Promise < string > {
523+ return this . getVersionOfJavaExecutableFromJavaHome ( Constants . JAVAC_EXECUTABLE_NAME ) ;
507524 }
508525
509- private async getJavacVersionFromJavaHome ( ) : Promise < string > {
510- let javacVersion : string = null ;
526+ private async getVersionOfJavaExecutableFromJavaHome ( javaExecutableName : string ) : Promise < string > {
527+ let javaExecutableVersion : string = null ;
511528
512529 try {
513530 const javaHome = process . env [ "JAVA_HOME" ] ;
514- const javacExecutableFile = this . hostInfo . isWindows ? `${ Constants . JAVAC_EXECUTABLE_NAME } .exe` : Constants . JAVAC_EXECUTABLE_NAME ;
531+ const javaExecutableFile = this . hostInfo . isWindows ? `${ javaExecutableName } .exe` : javaExecutableName ;
515532
516533 if ( javaHome ) {
517- const pathToJavaCompilerExecutable = path . join ( javaHome , "bin" , javacExecutableFile ) ;
518- if ( this . fileSystem . exists ( pathToJavaCompilerExecutable ) ) {
519- javacVersion = await this . executeJavacVersion ( pathToJavaCompilerExecutable ) ;
534+ const pathToJavaExecutable = path . join ( javaHome , "bin" , javaExecutableFile ) ;
535+ if ( this . fileSystem . exists ( pathToJavaExecutable ) ) {
536+ javaExecutableVersion = await this . getVersionOfJavaExecutable ( pathToJavaExecutable ) ;
520537 }
521538 }
522539 } catch ( err ) { /* intentionally left blank */ }
523540
524- return javacVersion ;
541+ return javaExecutableVersion ;
542+ }
543+
544+ private async getVersionOfJavaExecutableFromPath ( javaExecutableName : string ) : Promise < string > {
545+ let javaExecutableVersion : string = null ;
546+
547+ try {
548+ const detectionCommand = this . hostInfo . isWindows ? "where" : "which" ;
549+ // if this command succeeds, we have javac in the PATH. In case it is not there, it will throw an error.
550+ await this . childProcess . exec ( `${ detectionCommand } ${ javaExecutableName } ` ) ;
551+ javaExecutableVersion = await this . getVersionOfJavaExecutable ( javaExecutableName ) ;
552+ } catch ( err ) { /* intentionally left blank */ }
553+
554+ return javaExecutableVersion ;
525555 }
526556
527- private async executeJavacVersion ( pathToJavaCompilerExecutable : string ) : Promise < string > {
557+ private async getVersionOfJavaExecutable ( pathToExecutable : string ) : Promise < string > {
528558 try {
529- const output = await this . childProcess . exec ( `"${ pathToJavaCompilerExecutable } " -version` ) ;
559+ const output = await this . childProcess . exec ( `"${ pathToExecutable } " -version` ) ;
530560 return SysInfo . JAVA_COMPILER_VERSION_REGEXP . exec ( `${ output . stderr } ${ EOL } ${ output . stdout } ` ) [ 1 ] ;
531561 } catch ( err ) {
532562 return null ;
0 commit comments