Skip to content

Commit 0009dbc

Browse files
BrendanC23nvuillam
andauthored
Handle Absolute JAR Path (#106)
* Handle absolute paths to JAR file This commit fixes instances where the `jar` parameter is an absolute path to the JAR file. Previously, the path for ```js const java = new JavaCaller({ jar: "F:\\my-path\\file.jar", }); ``` would become `./F:\\my-path\\file.jar`. Now, it is kept as-is, allowing absolute paths to be provided. * Handle absolute paths to JAR file This commit fixes instances where the `jar` parameter is an absolute path to the JAR file. Previously, the path for ```js const java = new JavaCaller({ jar: "F:\\my-path\\file.jar", }); ``` would become `./F:\\my-path\\file.jar`. Now, it is kept as-is, allowing absolute paths to be provided. * Fix handling of absolute paths This commit fixes the handling of absolute paths, switching from `path.join()` to `path.isAbsolute()` to handle differing behavior for different platforms. `path.join()` works differently for Windows paths and Linux paths. `path.join(".", "C:\\") === "C:\\"` `path.join(".", "/home/test") === "home/test"` The latter is missing a leading `/` and thus is no longer an absolute path. As described in [this issue](GHSA-37v4-cwgp-x353), there was a NodeJS [security vulnerability](https://nodejs.org/en/blog/vulnerability/january-2025-security-releases) that caused the behavior of `path.join()` to be changed. Newer versions of Node will return a relative path in these instances, so `path.join(".", "C:\\") === ".\\C:\\"`. --------- Co-authored-by: Nicolas Vuillamy <nicolas.vuillamy@gmail.com>
1 parent fb429b7 commit 0009dbc

3 files changed

Lines changed: 17 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Fix to allow absolute path to JAR file
6+
57
## [4.2.0] 2025-02-23
68

79
- Upgrade to njre v1.4.2

lib/java-caller.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,12 @@ class JavaCaller {
207207
let allArgs = [];
208208
allArgs.push(...javaArgs);
209209
if (this.jar) {
210+
const jarPath = path.isAbsolute(this.jar) ? this.jar : path.join(this.rootPath, this.jar);
211+
210212
allArgs.push(
211213
...[
212214
"-jar",
213-
os.platform() === "win32" && windowsVerbatimArguments ? `"${this.rootPath}/${this.jar}"` : `${this.rootPath}/${this.jar}`,
215+
os.platform() === "win32" && windowsVerbatimArguments ? `"${jarPath}"` : `${jarPath}`,
214216
],
215217
);
216218
} else {

test/java-caller.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const { JavaCaller } = require('../lib/index');
44
const os = require("os");
55
const which = require("which");
6+
const path = require('path');
67

78
const {
89
beforeEachTestCase,
@@ -175,4 +176,15 @@ describe("Call with classes", () => {
175176
await javaCli.process();
176177
});
177178

179+
it("Should work with an absolute path", async () => {
180+
const absolutePath = path.join(process.cwd(), "test/java/jar/JavaCallerTesterRunnable.jar");
181+
182+
const java = new JavaCaller({
183+
jar: absolutePath,
184+
});
185+
const { status, stdout, stderr } = await java.run();
186+
187+
checkStatus(0, status, stdout, stderr);
188+
checkStdOutIncludes(`JavaCallerTester is called !`, stdout, stderr);
189+
});
178190
});

0 commit comments

Comments
 (0)