11import { spawn } from 'node:child_process' ;
2+ import path from 'node:path' ;
23import { JavaProcessError } from '../utils/errors.js' ;
34import { logger } from '../utils/logger.js' ;
45import { normalizeOptionalPath , normalizePath } from '../utils/path-converter.js' ;
56
7+ /**
8+ * Resolve the Java executable path.
9+ * Prefers JAVA_HOME/bin/java if JAVA_HOME is set, otherwise falls back to 'java' on PATH.
10+ */
11+ function getJavaExecutable ( ) : string {
12+ const javaHome = process . env . JAVA_HOME ;
13+ if ( javaHome ) {
14+ return path . join ( normalizePath ( javaHome ) , 'bin' , 'java' ) ;
15+ }
16+ return 'java' ;
17+ }
18+
619export interface JavaProcessOptions {
720 maxMemory ?: string ; // e.g., "2G"
821 minMemory ?: string ; // e.g., "512M"
@@ -70,7 +83,8 @@ export async function executeJavaProcess(
7083 javaArgs = [ ...baseJvmArgs , '-jar' , normalizedJarPath , ...normalizedArgs ] ;
7184 }
7285
73- logger . info ( `Executing Java: java ${ javaArgs . join ( ' ' ) } ` ) ;
86+ const javaExe = getJavaExecutable ( ) ;
87+ logger . info ( `Executing Java: ${ javaExe } ${ javaArgs . join ( ' ' ) } ` ) ;
7488
7589 return new Promise ( ( resolve , reject ) => {
7690 const spawnOptions : { stdio : [ 'ignore' , 'pipe' , 'pipe' ] ; cwd ?: string } = {
@@ -82,7 +96,7 @@ export async function executeJavaProcess(
8296 spawnOptions . cwd = normalizedCwd ;
8397 }
8498
85- const process = spawn ( 'java' , javaArgs , spawnOptions ) ;
99+ const process = spawn ( javaExe , javaArgs , spawnOptions ) ;
86100
87101 let stdout = '' ;
88102 let stderr = '' ;
@@ -144,7 +158,8 @@ export async function executeJavaProcess(
144158 */
145159export async function getJavaVersion ( ) : Promise < string > {
146160 return new Promise ( ( resolve , reject ) => {
147- const process = spawn ( 'java' , [ '-version' ] , { stdio : [ 'ignore' , 'pipe' , 'pipe' ] } ) ;
161+ const javaExe = getJavaExecutable ( ) ;
162+ const process = spawn ( javaExe , [ '-version' ] , { stdio : [ 'ignore' , 'pipe' , 'pipe' ] } ) ;
148163
149164 let output = '' ;
150165
0 commit comments