@@ -6,9 +6,10 @@ import * as path from 'path';
66import * as pathExists from 'path-exists' ;
77import * as expandHomeDir from 'expand-home-dir' ;
88import * as findJavaHome from 'find-java-home' ;
9+ import { Commands } from './commands' ;
910
1011const isWindows = process . platform . indexOf ( 'win' ) === 0 ;
11- const JAVAC_FILENAME = 'javac' + ( isWindows ? '.exe' : '' ) ;
12+ const JAVAC_FILENAME = 'javac' + ( isWindows ? '.exe' : '' ) ;
1213
1314export interface RequirementsData {
1415 java_home : string ;
@@ -18,8 +19,8 @@ export interface RequirementsData {
1819interface ErrorData {
1920 message : string ;
2021 label : string ;
21- openUrl : Uri ;
22- replaceClose : boolean ;
22+ command : string ;
23+ commandParam : any ;
2324}
2425/**
2526 * Resolves the requirements needed to run the extension.
@@ -31,65 +32,71 @@ interface ErrorData {
3132export async function resolveRequirements ( ) : Promise < RequirementsData > {
3233 let java_home = await checkJavaRuntime ( ) ;
3334 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 } ) ;
3536}
3637
3738function checkJavaRuntime ( ) : Promise < string > {
3839 return new Promise ( ( resolve , reject ) => {
39- let source : string ;
40- let javaHome : string = readJavaConfig ( ) ;
40+ let source : string ;
41+ let javaHome : string = readJavaConfig ( ) ;
4142 if ( javaHome ) {
42- source = 'The java.home variable defined in VS Code settings' ;
43+ source = 'java.home variable defined in VS Code settings' ;
4344 } else {
4445 javaHome = process . env [ 'JDK_HOME' ] ;
4546 if ( javaHome ) {
46- source = 'The JDK_HOME environment variable' ;
47+ source = 'JDK_HOME environment variable' ;
4748 } else {
4849 javaHome = process . env [ 'JAVA_HOME' ] ;
49- source = 'The JAVA_HOME environment variable' ;
50+ source = 'JAVA_HOME environment variable' ;
5051 }
5152 }
52- if ( javaHome ) {
53+ if ( javaHome ) {
5354 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 } )` ) ;
5657 }
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 ) ;
5966 }
6067 return resolve ( javaHome ) ;
6168 }
6269 //No settings, let's try to detect as last resort.
6370 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+ } ) ;
7178 } ) ;
7279}
7380
74- function readJavaConfig ( ) : string {
81+ function readJavaConfig ( ) : string {
7582 const config = workspace . getConfiguration ( ) ;
76- return config . get < string > ( 'java.home' , null ) ;
83+ return config . get < string > ( 'java.home' , null ) ;
7784}
7885
7986function checkJavaVersion ( java_home : string ) : Promise < number > {
8087 return new Promise ( ( resolve , reject ) => {
8188 cp . execFile ( java_home + '/bin/java' , [ '-version' ] , { } , ( error , stdout , stderr ) => {
8289 let javaVersion = parseMajorVersion ( stderr ) ;
83- if ( javaVersion < 8 ) {
90+ if ( javaVersion < 8 ) {
8491 openJDKDownload ( reject , 'Java 8 or more recent is required to run. Please download and install a recent JDK' ) ;
85- } else {
92+ } else {
8693 resolve ( javaVersion ) ;
8794 }
8895 } ) ;
8996 } ) ;
9097}
9198
92- export function parseMajorVersion ( content :string ) :number {
99+ export function parseMajorVersion ( content : string ) : number {
93100 let regexp = / v e r s i o n " ( .* ) " / g;
94101 let match = regexp . exec ( content ) ;
95102 if ( ! match ) {
@@ -119,7 +126,21 @@ function openJDKDownload(reject, cause) {
119126 reject ( {
120127 message : cause ,
121128 label : 'Get the Java Development Kit' ,
122- openUrl : Uri . parse ( jdkUrl ) ,
123- replaceClose : false
129+ command : Commands . OPEN_BROWSER ,
130+ commandParam : Uri . parse ( jdkUrl ) ,
124131 } ) ;
125132}
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