java-caller 3.3.2-beta202404281910.0 → 4.0.1-beta202405081245.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/java-caller.js +39 -24
  2. package/package.json +2 -2
@@ -10,7 +10,7 @@ const semver = require("semver");
10
10
 
11
11
  class JavaCaller {
12
12
  "use strict";
13
- minimumJavaVersion = 8;
13
+ minimumJavaVersion = os.platform() === "darwin" ? 11 : 8; // Mac starts at 11
14
14
  maximumJavaVersion;
15
15
  javaType;
16
16
  rootPath = ".";
@@ -30,6 +30,7 @@ class JavaCaller {
30
30
 
31
31
  javaHome;
32
32
  javaBin;
33
+ javaExecutableFromNodeJavaCaller;
33
34
 
34
35
  prevPath;
35
36
  prevJavaHome;
@@ -59,6 +60,7 @@ class JavaCaller {
59
60
  this.rootPath = opts.rootPath || this.rootPath;
60
61
  this.javaCallerSupportDir = `${os.homedir() + path.sep}.java-caller`;
61
62
  this.javaExecutable = opts.javaExecutable || process.env.JAVA_CALLER_JAVA_EXECUTABLE || this.javaExecutable;
63
+ this.javaExecutableFromNodeJavaCaller = null;
62
64
  this.additionalJavaArgs = opts.additionalJavaArgs || this.additionalJavaArgs;
63
65
  this.output = opts.output || this.output;
64
66
  }
@@ -85,7 +87,7 @@ class JavaCaller {
85
87
  this.commandJavaArgs = (runOptions.javaArgs || []).concat(this.additionalJavaArgs);
86
88
 
87
89
  let javaExe = runOptions.windowless ? this.javaExecutableWindowless : this.javaExecutable;
88
- if (javaExe.toLowerCase().includes(".exe") && !javaExe.includes(`'`)) {
90
+ if (javaExe.toLowerCase().includes(".exe") && javaExe.includes(" ") && !javaExe.includes(`'`)) {
89
91
  // Java executable has been overridden by caller : use it
90
92
  javaExe = `"${path.resolve(javaExe)}"`;
91
93
  } else if (javaExe === "java" || javaExe === "javaw") {
@@ -93,27 +95,27 @@ class JavaCaller {
93
95
  await this.manageJavaInstall();
94
96
  }
95
97
 
98
+ const javaExeToUse = this.javaExecutableFromNodeJavaCaller ?? javaExe;
96
99
  const classPathStr = this.buildClasspathStr();
97
-
98
100
  const javaArgs = this.buildArguments(classPathStr, (userArguments || []).concat(this.commandJavaArgs));
99
101
  let stdout = "";
100
102
  let stderr = "";
101
103
  let child;
102
104
  const prom = new Promise((resolve) => {
103
105
  // Spawn java command line
104
- debug(`Java command: ${javaExe} ${javaArgs.join(" ")}`);
106
+ debug(`Java command: ${javaExeToUse} ${javaArgs.join(" ")}`);
105
107
  const spawnOptions = {
106
108
  detached: runOptions.detached,
107
- cwd: javaExe === "java" || javaExe === "javaw" ? runOptions.cwd : undefined,
109
+ cwd: javaExeToUse === "java" || javaExeToUse === "javaw" ? runOptions.cwd : undefined,
108
110
  env: Object.assign({}, process.env),
109
111
  stdio: this.output === "console" ? "inherit" : runOptions.detached ? "ignore" : "pipe",
110
112
  windowsHide: true,
111
113
  windowsVerbatimArguments: runOptions.windowsVerbatimArguments,
112
114
  };
113
- if (javaExe.includes(" ")) {
115
+ if (javaExeToUse.includes(" ")) {
114
116
  spawnOptions.shell = true;
115
117
  }
116
- child = spawn(javaExe, javaArgs, spawnOptions);
118
+ child = spawn(javaExeToUse, javaArgs, spawnOptions);
117
119
 
118
120
  // Gather stdout and stderr if they must be returned
119
121
  if (spawnOptions.stdio === "pipe") {
@@ -220,6 +222,10 @@ class JavaCaller {
220
222
 
221
223
  // Install Java if the found java version is not matching the requirements
222
224
  async manageJavaInstall() {
225
+ if (this.javaExecutable !== 'java' && this.javaExecutable !== 'javaw') {
226
+ // Do not search/install java if its path is sent as argument
227
+ return;
228
+ }
223
229
  if (await this.getInstallInCache()) {
224
230
  return;
225
231
  }
@@ -239,15 +245,15 @@ class JavaCaller {
239
245
  this.javaHome = javaVersionHome;
240
246
  this.javaBin = javaVersionBin;
241
247
  await this.addJavaInPath();
248
+ this.setJavaExecutableFromNodeJavaCaller(this.javaBin);
242
249
  return;
243
250
  }
244
251
 
245
252
  // Inform user that the installation is pending
246
253
  const requiredMsg =
247
254
  this.minimumJavaVersion !== this.maximumJavaVersion
248
- ? `Java ${this.javaType ? this.javaType : "jre or jdk"} between ${this.minimumJavaVersion} and ${
249
- this.maximumJavaVersion
250
- } is required `
255
+ ? `Java ${this.javaType ? this.javaType : "jre or jdk"} between ${this.minimumJavaVersion} and ${this.maximumJavaVersion
256
+ } is required `
251
257
  : `Java ${this.javaType ? this.javaType : "jre or jdk"} ${this.minimumJavaVersion} is required`;
252
258
  console.log(requiredMsg);
253
259
  const javaVersionToInstall = this.maximumJavaVersion || this.minimumJavaVersion;
@@ -288,6 +294,7 @@ class JavaCaller {
288
294
  this.javaHome = java_home;
289
295
  this.javaBin = java_bin;
290
296
  await this.addJavaInPath();
297
+ this.setJavaExecutableFromNodeJavaCaller(this.javaBin);
291
298
  return true;
292
299
  }
293
300
  }
@@ -327,26 +334,25 @@ class JavaCaller {
327
334
  return await fse.readdir(javaInstallsTopDir)
328
335
  .then((items) =>
329
336
  items
330
- .filter((item) => fse.statSync(path.join(javaInstallsTopDir, item)).isDirectory())
331
- .map((folder) => {
332
- const version = semver.coerce(folder)
333
- return { version, folder }
334
- })
335
- .filter(({ version, folder }) => this.checkMatchingJavaVersion(version.major, folder))
336
- .map(({ version, folder }) => {
337
- const home = path.join(javaInstallsTopDir, folder);
338
- const bin = path.join(home, this.getPlatformBinPath());
339
- return { version, folder, home, bin }
340
- })
341
- .find(({ bin }) => fse.existsSync(bin))
337
+ .filter((item) => fse.statSync(path.join(javaInstallsTopDir, item)).isDirectory())
338
+ .map((folder) => {
339
+ const version = semver.coerce(folder)
340
+ return { version, folder }
341
+ })
342
+ .filter(({ version, folder }) => this.checkMatchingJavaVersion(version.major, folder))
343
+ .map(({ version, folder }) => {
344
+ const home = path.join(javaInstallsTopDir, folder);
345
+ const bin = path.join(home, this.getPlatformBinPath());
346
+ return { version, folder, home, bin }
347
+ })
348
+ .find(({ bin }) => fse.existsSync(bin))
342
349
  )
343
350
  .then((match) => {
344
351
  if (!match) return {};
345
352
  const { version, folder, home, bin } = match
346
353
 
347
354
  debug(
348
- `Found matching java bin: ${bin} for ${this.javaType ? this.javaType : "jre or jdk"} ${this.minimumJavaVersion}${
349
- this.maximumJavaVersion && this.maximumJavaVersion !== this.minimumJavaVersion ? " -> " + this.maximumJavaVersion : "+"
355
+ `Found matching java bin: ${bin} for ${this.javaType ? this.javaType : "jre or jdk"} ${this.minimumJavaVersion}${this.maximumJavaVersion && this.maximumJavaVersion !== this.minimumJavaVersion ? " -> " + this.maximumJavaVersion : "+"
350
356
  }`
351
357
  );
352
358
  this.addInCache(version.major, folder, home, bin);
@@ -415,6 +421,15 @@ class JavaCaller {
415
421
  return binPath;
416
422
  }
417
423
 
424
+ setJavaExecutableFromNodeJavaCaller(javaBinPath) {
425
+ this.javaExecutableFromNodeJavaCaller = path.join(
426
+ javaBinPath,
427
+ os.platform() === "win32" ? "java.exe" : "java");
428
+ if (this.javaExecutableFromNodeJavaCaller.includes(" ") && !this.javaExecutableFromNodeJavaCaller.startsWith('"')) {
429
+ this.javaExecutableFromNodeJavaCaller = `"${path.resolve(this.javaExecutableFromNodeJavaCaller)}"`
430
+ }
431
+ }
432
+
418
433
  fail(reason) {
419
434
  console.error(reason);
420
435
  this.status = 666;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "java-caller",
3
- "version": "3.3.2-beta202404281910.0",
3
+ "version": "4.0.1-beta202405081245.0",
4
4
  "description": "Library to easily call java from node sources. Automatically installs java if not present",
5
5
  "main": "./lib/index.js",
6
6
  "files": [
@@ -37,7 +37,7 @@
37
37
  "dependencies": {
38
38
  "debug": "^4.3.4",
39
39
  "fs-extra": "^11.1.1",
40
- "njre": "^1.1.0",
40
+ "njre": "^1.2.1",
41
41
  "semver": "^7.5.4"
42
42
  },
43
43
  "devDependencies": {