@@ -13,6 +13,7 @@ import { Constants } from "./constants";
13
13
14
14
export class SysInfo implements NativeScriptDoctor . ISysInfo {
15
15
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;
16
17
private static XCODE_VERSION_REGEXP = / X c o d e ( .* ) / ;
17
18
private static VERSION_REGEXP = / ( \d { 1 , } ) \. ( \d { 1 , } ) \. * ( [ \w - ] { 0 , } ) / m;
18
19
private static CLI_OUTPUT_VERSION_REGEXP = / ^ (?: \d + \. ) { 2 } \d + .* ?$ / m;
@@ -22,6 +23,9 @@ export class SysInfo implements NativeScriptDoctor.ISysInfo {
22
23
private monoVerRegExp = / v e r s i o n ( \d + [ . ] \d + [ . ] \d + ) / gm;
23
24
24
25
private javaCompilerVerCache : string ;
26
+ private javaVerCache : string ;
27
+ private javaVerJavaHomeCache : string ;
28
+ private javaVerPathCache : string ;
25
29
private xCodeVerCache : string ;
26
30
private npmVerCache : string ;
27
31
private nodeVerCache : string ;
@@ -57,15 +61,29 @@ export class SysInfo implements NativeScriptDoctor.ISysInfo {
57
61
58
62
public getJavaCompilerVersion ( ) : Promise < string > {
59
63
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 ) ;
69
87
} ) ;
70
88
}
71
89
@@ -490,6 +508,7 @@ export class SysInfo implements NativeScriptDoctor.ISysInfo {
490
508
491
509
result . dotNetVer = await this . hostInfo . dotNetVersion ( ) ;
492
510
result . javacVersion = await this . getJavaCompilerVersion ( ) ;
511
+ result . javaVersion = await this . getJavaVersion ( ) ;
493
512
result . adbVer = await this . getAdbVersion ( config && config . androidToolsInfo && config . androidToolsInfo . pathToAdb ) ;
494
513
result . androidInstalled = await this . isAndroidInstalled ( ) ;
495
514
result . monoVer = await this . getMonoVersion ( ) ;
@@ -499,4 +518,44 @@ export class SysInfo implements NativeScriptDoctor.ISysInfo {
499
518
return result ;
500
519
} ) ;
501
520
}
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
+ }
502
561
}
0 commit comments