@@ -13,6 +13,7 @@ import { Constants } from "./constants";
1313
1414export class SysInfo implements NativeScriptDoctor . ISysInfo {
1515 private static JAVA_COMPILER_VERSION_REGEXP = / ^ j a v a c ( .* ) / im;
16+ private static JAVA_VERSION_REGEXP = / ^ (?: (?: j a v a ) | (?: o p e n j d k ) ) .* ?\" ( .* ) \" / im;
1617 private static XCODE_VERSION_REGEXP = / X c o d e ( .* ) / ;
1718 private static VERSION_REGEXP = / ( \d { 1 , } ) \. ( \d { 1 , } ) \. * ( [ \w - ] { 0 , } ) / m;
1819 private static CLI_OUTPUT_VERSION_REGEXP = / ^ (?: \d + \. ) { 2 } \d + .* ?$ / m;
@@ -22,6 +23,9 @@ export class SysInfo implements NativeScriptDoctor.ISysInfo {
2223 private monoVerRegExp = / v e r s i o n ( \d + [ . ] \d + [ . ] \d + ) / gm;
2324
2425 private javaCompilerVerCache : string ;
26+ private javaVerCache : string ;
27+ private javaVerJavaHomeCache : string ;
28+ private javaVerPathCache : string ;
2529 private xCodeVerCache : string ;
2630 private npmVerCache : string ;
2731 private nodeVerCache : string ;
@@ -57,15 +61,29 @@ export class SysInfo implements NativeScriptDoctor.ISysInfo {
5761
5862 public getJavaCompilerVersion ( ) : Promise < string > {
5963 return this . getValueForProperty ( ( ) => this . javaCompilerVerCache , async ( ) : Promise < string > => {
60- const javaCompileExecutableName = "javac" ;
61- const javaHome = process . env [ "JAVA_HOME" ] ;
62- const pathToJavaCompilerExecutable = javaHome ? path . join ( javaHome , "bin" , javaCompileExecutableName ) : javaCompileExecutableName ;
63- try {
64- const output = await this . childProcess . exec ( `"${ pathToJavaCompilerExecutable } " -version` ) ;
65- return SysInfo . JAVA_COMPILER_VERSION_REGEXP . exec ( `${ output . stderr } ${ EOL } ${ output . stdout } ` ) [ 1 ] ;
66- } catch ( err ) {
67- return null ;
68- }
64+ const javacVersion = ( await this . getVersionOfJavaExecutableFromJavaHome ( Constants . JAVAC_EXECUTABLE_NAME , SysInfo . JAVA_COMPILER_VERSION_REGEXP ) ) ||
65+ ( await this . getVersionOfJavaExecutableFromPath ( Constants . JAVAC_EXECUTABLE_NAME , SysInfo . JAVA_COMPILER_VERSION_REGEXP ) ) ;
66+
67+ return javacVersion ;
68+ } ) ;
69+ }
70+
71+ public getJavaVersion ( ) : Promise < string > {
72+ return this . getValueForProperty ( ( ) => this . javaVerCache , async ( ) : Promise < string > => {
73+ const javaVersion = ( await this . getJavaVersionFromJavaHome ( ) ) || ( await this . getJavaVersionFromPath ( ) ) ;
74+ return javaVersion ;
75+ } ) ;
76+ }
77+
78+ public getJavaVersionFromPath ( ) : Promise < string > {
79+ return this . getValueForProperty ( ( ) => this . javaVerPathCache , ( ) : Promise < string > => {
80+ return this . getVersionOfJavaExecutableFromPath ( Constants . JAVA_EXECUTABLE_NAME , SysInfo . JAVA_VERSION_REGEXP ) ;
81+ } ) ;
82+ }
83+
84+ public getJavaVersionFromJavaHome ( ) : Promise < string > {
85+ return this . getValueForProperty ( ( ) => this . javaVerJavaHomeCache , ( ) : Promise < string > => {
86+ return this . getVersionOfJavaExecutableFromJavaHome ( Constants . JAVA_EXECUTABLE_NAME , SysInfo . JAVA_VERSION_REGEXP ) ;
6987 } ) ;
7088 }
7189
@@ -490,6 +508,7 @@ export class SysInfo implements NativeScriptDoctor.ISysInfo {
490508
491509 result . dotNetVer = await this . hostInfo . dotNetVersion ( ) ;
492510 result . javacVersion = await this . getJavaCompilerVersion ( ) ;
511+ result . javaVersion = await this . getJavaVersion ( ) ;
493512 result . adbVer = await this . getAdbVersion ( config && config . androidToolsInfo && config . androidToolsInfo . pathToAdb ) ;
494513 result . androidInstalled = await this . isAndroidInstalled ( ) ;
495514 result . monoVer = await this . getMonoVersion ( ) ;
@@ -499,4 +518,44 @@ export class SysInfo implements NativeScriptDoctor.ISysInfo {
499518 return result ;
500519 } ) ;
501520 }
521+
522+ private async getVersionOfJavaExecutableFromJavaHome ( javaExecutableName : string , regExp : RegExp ) : Promise < string > {
523+ let javaExecutableVersion : string = null ;
524+
525+ try {
526+ const javaHome = process . env [ "JAVA_HOME" ] ;
527+ const javaExecutableFile = this . hostInfo . isWindows ? `${ javaExecutableName } .exe` : javaExecutableName ;
528+
529+ if ( javaHome ) {
530+ const pathToJavaExecutable = path . join ( javaHome , "bin" , javaExecutableFile ) ;
531+ if ( this . fileSystem . exists ( pathToJavaExecutable ) ) {
532+ javaExecutableVersion = await this . getVersionOfJavaExecutable ( pathToJavaExecutable , regExp ) ;
533+ }
534+ }
535+ } catch ( err ) { /* intentionally left blank */ }
536+
537+ return javaExecutableVersion ;
538+ }
539+
540+ private async getVersionOfJavaExecutableFromPath ( javaExecutableName : string , regExp : RegExp ) : Promise < string > {
541+ let javaExecutableVersion : string = null ;
542+
543+ try {
544+ const detectionCommand = this . hostInfo . isWindows ? "where" : "which" ;
545+ // if this command succeeds, we have javac in the PATH. In case it is not there, it will throw an error.
546+ await this . childProcess . exec ( `${ detectionCommand } ${ javaExecutableName } ` ) ;
547+ javaExecutableVersion = await this . getVersionOfJavaExecutable ( javaExecutableName , regExp ) ;
548+ } catch ( err ) { /* intentionally left blank */ }
549+
550+ return javaExecutableVersion ;
551+ }
552+
553+ private async getVersionOfJavaExecutable ( executable : string , regExp : RegExp ) : Promise < string > {
554+ try {
555+ const output = await this . childProcess . exec ( `"${ executable } " -version` ) ;
556+ return regExp . exec ( `${ output . stderr } ${ EOL } ${ output . stdout } ` ) [ 1 ] ;
557+ } catch ( err ) {
558+ return null ;
559+ }
560+ }
502561}
0 commit comments