@@ -6,9 +6,10 @@ import * as path from 'path';
6
6
import * as pathExists from 'path-exists' ;
7
7
import * as expandHomeDir from 'expand-home-dir' ;
8
8
import * as findJavaHome from 'find-java-home' ;
9
+ import { Commands } from './commands' ;
9
10
10
11
const isWindows = process . platform . indexOf ( 'win' ) === 0 ;
11
- const JAVAC_FILENAME = 'javac' + ( isWindows ? '.exe' : '' ) ;
12
+ const JAVAC_FILENAME = 'javac' + ( isWindows ? '.exe' : '' ) ;
12
13
13
14
export interface RequirementsData {
14
15
java_home : string ;
@@ -18,8 +19,8 @@ export interface RequirementsData {
18
19
interface ErrorData {
19
20
message : string ;
20
21
label : string ;
21
- openUrl : Uri ;
22
- replaceClose : boolean ;
22
+ command : string ;
23
+ commandParam : any ;
23
24
}
24
25
/**
25
26
* Resolves the requirements needed to run the extension.
@@ -31,65 +32,71 @@ interface ErrorData {
31
32
export async function resolveRequirements ( ) : Promise < RequirementsData > {
32
33
let java_home = await checkJavaRuntime ( ) ;
33
34
let javaVersion = await checkJavaVersion ( java_home ) ;
34
- return Promise . resolve ( { 'java_home' : java_home , 'java_version' : javaVersion } ) ;
35
+ return Promise . resolve ( { 'java_home' : java_home , 'java_version' : javaVersion } ) ;
35
36
}
36
37
37
38
function checkJavaRuntime ( ) : Promise < string > {
38
39
return new Promise ( ( resolve , reject ) => {
39
- let source : string ;
40
- let javaHome : string = readJavaConfig ( ) ;
40
+ let source : string ;
41
+ let javaHome : string = readJavaConfig ( ) ;
41
42
if ( javaHome ) {
42
- source = 'The java.home variable defined in VS Code settings' ;
43
+ source = 'java.home variable defined in VS Code settings' ;
43
44
} else {
44
45
javaHome = process . env [ 'JDK_HOME' ] ;
45
46
if ( javaHome ) {
46
- source = 'The JDK_HOME environment variable' ;
47
+ source = 'JDK_HOME environment variable' ;
47
48
} else {
48
49
javaHome = process . env [ 'JAVA_HOME' ] ;
49
- source = 'The JAVA_HOME environment variable' ;
50
+ source = 'JAVA_HOME environment variable' ;
50
51
}
51
52
}
52
- if ( javaHome ) {
53
+ if ( javaHome ) {
53
54
javaHome = expandHomeDir ( javaHome ) ;
54
- if ( ! pathExists . sync ( javaHome ) ) {
55
- openJDKDownload ( reject , source + ' points to a missing folder' ) ;
55
+ if ( ! pathExists . sync ( javaHome ) ) {
56
+ invalidJavaHome ( reject , `The ${ source } points to a missing or inaccessible folder ( ${ javaHome } )` ) ;
56
57
}
57
- if ( ! pathExists . sync ( path . resolve ( javaHome , 'bin' , JAVAC_FILENAME ) ) ) {
58
- openJDKDownload ( reject , source + ' does not point to a JDK.' ) ;
58
+ else if ( ! pathExists . sync ( path . resolve ( javaHome , 'bin' , JAVAC_FILENAME ) ) ) {
59
+ let msg : string ;
60
+ if ( pathExists . sync ( path . resolve ( javaHome , JAVAC_FILENAME ) ) ) {
61
+ msg = `'bin' should be removed from the ${ source } (${ javaHome } )` ;
62
+ } else {
63
+ msg = `The ${ source } (${ javaHome } ) does not point to a JDK.`
64
+ }
65
+ invalidJavaHome ( reject , msg ) ;
59
66
}
60
67
return resolve ( javaHome ) ;
61
68
}
62
69
//No settings, let's try to detect as last resort.
63
70
findJavaHome ( function ( err , home ) {
64
- if ( err ) {
65
- openJDKDownload ( reject , 'Java runtime could not be located' ) ;
66
- }
67
- else {
68
- resolve ( home ) ;
69
- }
70
- } ) ;
71
+ if ( err ) {
72
+ openJDKDownload ( reject , 'Java runtime (JDK, not JRE) could not be located' ) ;
73
+ }
74
+ else {
75
+ resolve ( home ) ;
76
+ }
77
+ } ) ;
71
78
} ) ;
72
79
}
73
80
74
- function readJavaConfig ( ) : string {
81
+ function readJavaConfig ( ) : string {
75
82
const config = workspace . getConfiguration ( ) ;
76
- return config . get < string > ( 'java.home' , null ) ;
83
+ return config . get < string > ( 'java.home' , null ) ;
77
84
}
78
85
79
86
function checkJavaVersion ( java_home : string ) : Promise < number > {
80
87
return new Promise ( ( resolve , reject ) => {
81
88
cp . execFile ( java_home + '/bin/java' , [ '-version' ] , { } , ( error , stdout , stderr ) => {
82
89
let javaVersion = parseMajorVersion ( stderr ) ;
83
- if ( javaVersion < 8 ) {
90
+ if ( javaVersion < 8 ) {
84
91
openJDKDownload ( reject , 'Java 8 or more recent is required to run. Please download and install a recent JDK' ) ;
85
- } else {
92
+ } else {
86
93
resolve ( javaVersion ) ;
87
94
}
88
95
} ) ;
89
96
} ) ;
90
97
}
91
98
92
- export function parseMajorVersion ( content :string ) :number {
99
+ export function parseMajorVersion ( content : string ) : number {
93
100
let regexp = / v e r s i o n " ( .* ) " / g;
94
101
let match = regexp . exec ( content ) ;
95
102
if ( ! match ) {
@@ -119,7 +126,21 @@ function openJDKDownload(reject, cause) {
119
126
reject ( {
120
127
message : cause ,
121
128
label : 'Get the Java Development Kit' ,
122
- openUrl : Uri . parse ( jdkUrl ) ,
123
- replaceClose : false
129
+ command : Commands . OPEN_BROWSER ,
130
+ commandParam : Uri . parse ( jdkUrl ) ,
124
131
} ) ;
125
132
}
133
+
134
+ function invalidJavaHome ( reject , cause : string ) {
135
+ if ( cause . indexOf ( "java.home" ) > - 1 ) {
136
+ reject ( {
137
+ message : cause ,
138
+ label : 'Open settings' ,
139
+ command : Commands . OPEN_JSON_SETTINGS
140
+ } ) ;
141
+ } else {
142
+ reject ( {
143
+ message : cause ,
144
+ } ) ;
145
+ }
146
+ }
0 commit comments