@@ -21,20 +21,46 @@ const javaToolOptions = process.env.JAVA_TOOL_OPTIONS || '';
2121// Check if debugging is enabled
2222const isDebugEnabled = javaToolOptions . includes ( 'jdwp' ) && endpointFile ;
2323
24+ // Helper function to find java command
25+ function getJavaCommand ( ) {
26+ const javaHome = process . env . JAVA_HOME ;
27+
28+ // Try JAVA_HOME first
29+ if ( javaHome ) {
30+ const javaPath = path . join ( javaHome , 'bin' , 'java' ) ;
31+ const javaPathExe = process . platform === 'win32' ? `${ javaPath } .exe` : javaPath ;
32+
33+ // Check if the file exists
34+ if ( fs . existsSync ( javaPathExe ) ) {
35+ return javaPath ;
36+ }
37+ if ( fs . existsSync ( javaPath ) ) {
38+ return javaPath ;
39+ }
40+
41+ console . warn ( `[Java Debug] JAVA_HOME is set to '${ javaHome } ', but java command not found there. Falling back to PATH.` ) ;
42+ }
43+
44+ // Fall back to 'java' in PATH
45+ return 'java' ;
46+ }
47+
48+ const javaCmd = getJavaCommand ( ) ;
49+
2450if ( ! isDebugEnabled ) {
2551 // No debugging, just run java normally
26- const javaHome = process . env . JAVA_HOME ;
27- const javaCmd = javaHome ? path . join ( javaHome , 'bin' , 'java' ) : 'java' ;
2852 const child = spawn ( javaCmd , process . argv . slice ( 2 ) , {
2953 stdio : 'inherit' ,
3054 shell : false
3155 } ) ;
3256 child . on ( 'exit' , ( code ) => process . exit ( code || 0 ) ) ;
57+ child . on ( 'error' , ( err ) => {
58+ console . error ( `[Java Debug] Failed to start java: ${ err . message } ` ) ;
59+ console . error ( `[Java Debug] Make sure Java is installed and either JAVA_HOME is set correctly or 'java' is in your PATH.` ) ;
60+ process . exit ( 1 ) ;
61+ } ) ;
3362} else {
3463 // Debugging enabled, capture JDWP port
35- const javaHome = process . env . JAVA_HOME ;
36- const javaCmd = javaHome ? path . join ( javaHome , 'bin' , 'java' ) : 'java' ;
37-
3864 const child = spawn ( javaCmd , process . argv . slice ( 2 ) , {
3965 stdio : [ 'inherit' , 'pipe' , 'pipe' ] ,
4066 shell : false
0 commit comments