Skip to content

Commit

Permalink
Addition of windowless option (#66)
Browse files Browse the repository at this point in the history
Added a new option to call "javaw" instead of "java", only for Windows
  • Loading branch information
SotirisVas committed Jan 28, 2024
1 parent 3292af6 commit 74e1c84
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Add option for using javaw instead of java on run for Windows, by [@SotirisVas](https://github.com/SotirisVas) in [#65](https://github.com/nvuillam/node-java-caller/issues/65)

## [3.2.0] 2023-11-26

- Upgrade njre to v1.1.0 (now handles Mac M1)
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Example: `["-Xms256m", "--someflagwithvalue myVal", "-c"]`
| [cwd](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options) | You can override cwd of spawn called by JavaCaller runner | `process.cwd()` | `some/other/cwd/folder` |
| javaArgs | List of arguments for JVM only, not the JAR or the class | `[]` | `['--add-opens=java.base/java.lang=ALL-UNNAMED']` |
| [windowsVerbatimArguments](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options) | No quoting or escaping of arguments is done on Windows. Ignored on Unix. This is set to true automatically when shell is specified and is CMD. | `true` | `false` |
| [windowless](https://docs.oracle.com/en/java/javase/17/docs/specs/man/java.html#:~:text=main()%20method.-,javaw,information%20if%20a%20launch%20fails.) | If windowless is true, JavaCaller calls javaw instead of java to not create any windows, useful when using detached on Windows. Ignored on Unix. | false | true

## Examples

Expand Down Expand Up @@ -137,6 +138,16 @@ const { status, stdout, stderr, childJavaProcess } = await java.run(['--sleep'],
childJavaProcess.kill('SIGINT');
```

Call a windowless java process

```javascript
const java = new JavaCaller({
classPath: 'test/java/dist',
mainClass: 'com.nvuillam.javacaller.JavaCallerTester'
});
const { status, stdout, stderr } = await java.run(['--sleep'], { windowless: true });
```

You can see **more examples in** [**test methods**](https://github.com/nvuillam/node-java-caller/blob/master/test/java-caller.test.js)

## TROUBLESHOOTING
Expand Down
10 changes: 6 additions & 4 deletions lib/java-caller.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class JavaCaller {

javaSupportDir;
javaExecutable = "java";
javaExecutableWindowless = "javaw";
additionalJavaArgs = [];
commandJavaArgs = [];

Expand Down Expand Up @@ -80,13 +81,14 @@ class JavaCaller {
runOptions.cwd = typeof runOptions.cwd === "undefined" ? process.cwd() : runOptions.cwd;
runOptions.stdoutEncoding = typeof runOptions.stdoutEncoding === "undefined" ? "utf8" : runOptions.stdoutEncoding;
runOptions.windowsVerbatimArguments = typeof runOptions.windowsVerbatimArguments === "undefined" ? true : runOptions.windowsVerbatimArguments;
runOptions.windowless = typeof runOptions.windowless === "undefined" ? false : os.platform() !== "win32" ? false : runOptions.windowless;
this.commandJavaArgs = (runOptions.javaArgs || []).concat(this.additionalJavaArgs);

let javaExe = this.javaExecutable;
let javaExe = runOptions.windowless ? this.javaExecutableWindowless : this.javaExecutable;
if (javaExe.toLowerCase().includes(".exe") && !javaExe.includes(`'`)) {
// Java executable has been overridden by caller : use it
javaExe = `"${path.resolve(javaExe)}"`;
} else if (javaExe === "java") {
} else if (javaExe === "java" || javaExe === "javaw") {
// Check if matching java version is present, install and update PATH if it is not
await this.manageJavaInstall();
}
Expand All @@ -102,7 +104,7 @@ class JavaCaller {
debug(`Java command: ${javaExe} ${javaArgs.join(" ")}`);
const spawnOptions = {
detached: runOptions.detached,
cwd: javaExe === "java" ? runOptions.cwd : undefined,
cwd: javaExe === "java" || javaExe === "javaw" ? runOptions.cwd : undefined,
env: Object.assign({}, process.env),
stdio: this.output === "console" ? "inherit" : runOptions.detached ? "ignore" : "pipe",
windowsHide: true,
Expand Down Expand Up @@ -426,4 +428,4 @@ class JavaCaller {
}
}

module.exports = { JavaCaller };
module.exports = { JavaCaller };
10 changes: 10 additions & 0 deletions test/java-caller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ describe("Call with classes", () => {
checkStatus(0, status, stdout, stderr);
});

it("should call JavaCallerTester.class using javaw", async () => {
const java = new JavaCaller({
classPath: 'test/java/dist',
mainClass: 'com.nvuillam.javacaller.JavaCallerTester'
});
const { status, stdout, stderr } = await java.run(['--sleep'], { windowless: true });
checkStatus(0, status, stdout, stderr);
});


it("should call JavaCallerTester.class with proper stdout encoding", async () => {
const java = new JavaCaller({
classPath: 'test/java/dist',
Expand Down

0 comments on commit 74e1c84

Please sign in to comment.