@@ -8,7 +8,7 @@ const pathExists = require('path-exists');
88const expandHomeDir = require ( 'expand-home-dir' ) ;
99const findJavaHome = require ( 'find-java-home' ) ;
1010const isWindows = process . platform . indexOf ( 'win' ) === 0 ;
11- const JAVAC_FILENAME = 'javac ' + ( isWindows ?'.exe' :'' ) ;
11+ const JAVA_FILENAME = 'java ' + ( isWindows ?'.exe' :'' ) ;
1212
1313export interface RequirementsData {
1414 java_home : string ;
@@ -36,14 +36,37 @@ export async function resolveRequirements(): Promise<RequirementsData> {
3636
3737function checkJavaRuntime ( ) : Promise < string > {
3838 return new Promise ( ( resolve , reject ) => {
39+ let source : string ;
40+ let javaHome : string = readXMLJavaHomeConfig ( ) ;
41+
42+ if ( javaHome ) {
43+ source = 'The xml.java.home variable defined in VS Code settings' ;
44+ } else {
45+ javaHome = readJavaHomeConfig ( ) ;
46+ if ( javaHome ) {
47+ source = 'The java.home variable defined in VS Code settings' ;
48+ } else {
49+ javaHome = process . env [ 'JDK_HOME' ] ;
50+ if ( javaHome ) {
51+ source = 'The JDK_HOME environment variable' ;
52+ } else {
53+ javaHome = process . env [ 'JAVA_HOME' ] ;
54+ source = 'The JAVA_HOME environment variable' ;
55+ }
56+ }
57+ }
3958
40- checkXMLJavaHome ( resolve , reject ) ;
41- checkJavaHome ( resolve , reject ) ;
42- checkEnvVariable ( 'JDK_HOME' , resolve , reject ) ;
43- checkEnvVariable ( 'JAVA_HOME' , resolve , reject ) ;
44-
59+ if ( javaHome ) {
60+ javaHome = expandHomeDir ( javaHome ) ;
61+ if ( ! pathExists . sync ( javaHome ) ) {
62+ openJDKDownload ( reject , source + ' points to a missing folder' ) ;
63+ } else if ( ! pathExists . sync ( path . resolve ( javaHome , 'bin' , JAVA_FILENAME ) ) ) {
64+ openJDKDownload ( reject , source + ' does not point to a Java runtime.' ) ;
65+ }
66+ return resolve ( javaHome ) ;
67+ }
4568 //No settings, let's try to detect as last resort.
46- findJavaHome ( function ( err , home ) {
69+ findJavaHome ( { allowJre : true } , function ( err , home ) {
4770 if ( err ) {
4871 openJDKDownload ( reject , 'Java runtime could not be located.' ) ;
4972 }
@@ -54,31 +77,6 @@ function checkJavaRuntime(): Promise<string> {
5477 } ) ;
5578}
5679
57- function checkXMLJavaHome ( resolve , reject ) {
58- const javaHome = readXMLJavaHomeConfig ( ) ;
59- if ( ! javaHome ) {
60- return ;
61- }
62- const source = 'The xml.java.home variable defined in VS Code settings' ;
63- handleJavaPath ( javaHome , source , resolve , reject ) ;
64- }
65-
66- function checkJavaHome ( resolve , reject ) {
67- const javaHome = readJavaHomeConfig ( ) ;
68- if ( ! javaHome ) {
69- return ;
70- }
71- const source = 'The java.home variable defined in VS Code settings' ;
72- handleJavaPath ( javaHome , source , resolve , reject ) ;
73- }
74-
75- function checkEnvVariable ( name : string , resolve , reject ) {
76- if ( ! process . env [ name ] ) {
77- return ;
78- }
79- const source = `The ${ name } environment variable` ;
80- handleJavaPath ( process . env [ name ] , source , resolve , reject ) ;
81- }
8280
8381function readXMLJavaHomeConfig ( ) : string {
8482 return workspace . getConfiguration ( 'xml' ) . java . home ;
@@ -88,18 +86,6 @@ function readJavaHomeConfig() : string {
8886 const config = workspace . getConfiguration ( ) ;
8987 return config . get < string > ( 'java.home' , null ) ;
9088}
91-
92- function handleJavaPath ( javaHome : string , source : string , resolve , reject ) {
93- const javaHomeExpanded = expandHomeDir ( javaHome ) ;
94-
95- if ( ! pathExists . sync ( javaHomeExpanded ) ) {
96- openJDKDownload ( reject , source + ' points to a missing folder.' ) ;
97- }
98- if ( ! pathExists . sync ( path . resolve ( javaHomeExpanded , 'bin' , JAVAC_FILENAME ) ) ) {
99- openJDKDownload ( reject , source + ' does not point to a JDK.' ) ;
100- }
101- return resolve ( javaHomeExpanded ) ;
102- }
10389
10490function checkJavaVersion ( java_home : string ) : Promise < number > {
10591 return new Promise ( ( resolve , reject ) => {
0 commit comments