Skip to content

Commit 8735d86

Browse files
authored
Merge pull request #6 from Narazaka/fix/java-home
fix(java): resolve Java executable path using JAVA_HOME or fallback to 'java'
2 parents cf44fb7 + 1298910 commit 8735d86

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

src/java/java-process.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
import { spawn } from 'node:child_process';
2+
import path from 'node:path';
23
import { JavaProcessError } from '../utils/errors.js';
34
import { logger } from '../utils/logger.js';
45
import { 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+
619
export 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
*/
145159
export 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

Comments
 (0)