java-caller 4.3.2 → 4.4.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.
package/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
  [![Version](https://img.shields.io/npm/v/java-caller.svg)](https://www.npmjs.com/package/java-caller)
5
5
  [![Downloads/week](https://img.shields.io/npm/dw/java-caller.svg)](https://npmjs.org/package/java-caller)
6
6
  [![Downloads/total](https://img.shields.io/npm/dt/java-caller.svg)](https://npmjs.org/package/java-caller)<!-- gh-dependents-info-used-by-start -->
7
- [![Generated by github-dependents-info](https://img.shields.io/static/v1?label=Used%20by&message=159&color=informational&logo=slickpic)](https://github.com/nvuillam/node-java-caller/blob/main/docs/github-dependents-info.md)<!-- gh-dependents-info-used-by-end -->
7
+ [![Generated by github-dependents-info](https://img.shields.io/static/v1?label=Used%20by&message=162&color=informational&logo=slickpic)](https://github.com/nvuillam/node-java-caller/blob/main/docs/github-dependents-info.md)<!-- gh-dependents-info-used-by-end -->
8
8
  [![Mega-Linter](https://github.com/nvuillam/node-java-caller/workflows/Mega-Linter/badge.svg)](https://github.com/nvuillam/mega-linter#readme)
9
9
  [![codecov](https://codecov.io/gh/nvuillam/node-java-caller/branch/master/graph/badge.svg)](https://codecov.io/gh/nvuillam/node-java-caller)
10
10
  [![GitHub contributors](https://img.shields.io/github/contributors/nvuillam/node-java-caller.svg)](https://gitHub.com/nvuillam/node-java-caller/graphs/contributors/)
@@ -65,13 +65,16 @@ Example: `["-Xms256m", "--someflagwithvalue myVal", "-c"]`
65
65
 
66
66
  | Parameter | Description | Default | Example |
67
67
  |-----------|-------------|---------|---------|
68
- | [detached](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options) | If set to true, node will node wait for the java command to be completed.<br/>In that case, `childJavaProcess` property will be returned, but `stdout` and `stderr` may be empty, except if an error is triggered at command execution | `false` | `true`
68
+ | [detached](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options) | If set to true, node will not wait for the java command to be completed.<br/>In that case, `childJavaProcess` property will be returned, but `stdout` and `stderr` may be empty, except if an error is triggered at command execution | `false` | `true`
69
69
  | [stdoutEncoding](https://nodejs.org/api/stream.html#readablesetencodingencoding) | Adds control on spawn process stdout | `utf8` | `ucs2` |
70
70
  | waitForErrorMs | If detached is true, number of milliseconds to wait to detect an error before exiting JavaCaller run | `500` | `2000` |
71
71
  | [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` |
72
72
  | javaArgs | List of arguments for JVM only, not the JAR or the class | `[]` | `['--add-opens=java.base/java.lang=ALL-UNNAMED']` |
73
73
  | [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` |
74
74
  | [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
75
+ | [windowsHide](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options) | On Windows, hide the subprocess console window that would normally be created. Set to `false` if you need Java UI dialogs to be visible (e.g., print dialogs). Ignored on Unix. | `true` | `false`
76
+ | [timeout](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options) | In milliseconds the maximum amount of time the process is allowed to run. | `undefined` | `1000`
77
+ | [killSignal](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options) | The signal value to be used when the spawned process will be killed by timeout or abort signal. | `SIGTERM` | `SIGINT`
75
78
 
76
79
  ## Examples
77
80
 
@@ -147,6 +150,23 @@ const java = new JavaCaller({
147
150
  const { status, stdout, stderr } = await java.run(['--sleep'], { windowless: true });
148
151
  ```
149
152
 
153
+ Call java process with visible windows (e.g., for print dialogs)
154
+
155
+ ```javascript
156
+ const java = new JavaCaller({
157
+ classPath: 'test/java/dist',
158
+ mainClass: 'com.nvuillam.javacaller.JavaCallerTester'
159
+ });
160
+ // Set windowsHide to false to allow Java UI dialogs to be visible
161
+ const { status, stdout, stderr } = await java.run([], { windowsHide: false });
162
+ ```
163
+
164
+ When using CLI mode with `--no-windows-hide` flag:
165
+
166
+ ```bash
167
+ node index.js --no-windows-hide
168
+ ```
169
+
150
170
  You can see **more examples in** [**test methods**](https://github.com/nvuillam/node-java-caller/blob/master/test/java-caller.test.js)
151
171
 
152
172
  ## TROUBLESHOOTING
package/lib/cli.js CHANGED
@@ -26,7 +26,18 @@ class JavaCallerCli {
26
26
  const java = new JavaCaller(this.javaCallerOptions);
27
27
  const args = [...process.argv];
28
28
  args.splice(0, 2);
29
- const { status } = await java.run(args);
29
+
30
+ // Parse --no-windows-hide flag (remove all occurrences)
31
+ const runOptions = {};
32
+ const filteredArgs = args.filter(arg => {
33
+ if (arg === '--no-windows-hide') {
34
+ runOptions.windowsHide = false;
35
+ return false; // Remove from args
36
+ }
37
+ return true; // Keep in args
38
+ });
39
+
40
+ const { status } = await java.run(filteredArgs, runOptions);
30
41
  process.exitCode = status;
31
42
  }
32
43
  }
package/lib/index.d.ts ADDED
@@ -0,0 +1,224 @@
1
+
2
+ /* eslint-disable no-unused-vars */
3
+ /// <reference types="node" />
4
+
5
+ /**
6
+ * Options for JavaCaller constructor
7
+ */
8
+ export interface JavaCallerOptions {
9
+ /**
10
+ * Path to executable jar file
11
+ */
12
+ jar?: string;
13
+
14
+ /**
15
+ * If jar parameter is not set, classpath to use.
16
+ * Use : as separator (it will be converted if run on Windows), or use a string array.
17
+ */
18
+ classPath?: string | string[];
19
+
20
+ /**
21
+ * Set to true if classpaths should not be based on the rootPath
22
+ */
23
+ useAbsoluteClassPaths?: boolean;
24
+
25
+ /**
26
+ * If classPath set, main class to call
27
+ */
28
+ mainClass?: string;
29
+
30
+ /**
31
+ * Minimum java version to be used to call java command.
32
+ * If the java version found on machine is lower, java-caller will try to install and use the appropriate one
33
+ * @default 8 (11 on macOS)
34
+ */
35
+ minimumJavaVersion?: number;
36
+
37
+ /**
38
+ * Maximum java version to be used to call java command.
39
+ * If the java version found on machine is upper, java-caller will try to install and use the appropriate one
40
+ */
41
+ maximumJavaVersion?: number;
42
+
43
+ /**
44
+ * jre or jdk (if not defined and installation is required, jre will be installed)
45
+ */
46
+ javaType?: "jre" | "jdk";
47
+
48
+ /**
49
+ * If classPath elements are not relative to the current folder, you can define a root path.
50
+ * You may use __dirname if you classes / jars are in your module folder
51
+ * @default "." (current folder)
52
+ */
53
+ rootPath?: string;
54
+
55
+ /**
56
+ * You can force to use a defined java executable, instead of letting java-caller find/install one.
57
+ * Can also be defined with env var JAVA_CALLER_JAVA_EXECUTABLE
58
+ */
59
+ javaExecutable?: string;
60
+
61
+ /**
62
+ * Additional parameters for JVM that will be added in every JavaCaller instance runs
63
+ */
64
+ additionalJavaArgs?: string[];
65
+
66
+ /**
67
+ * Output mode: "none" or "console"
68
+ * @default "none"
69
+ */
70
+ output?: "none" | "console";
71
+ }
72
+
73
+ /**
74
+ * Options for JavaCaller run method
75
+ */
76
+ export interface JavaCallerRunOptions {
77
+ /**
78
+ * If set to true, node will not wait for the java command to be completed.
79
+ * In that case, childJavaProcess property will be returned, but stdout and stderr may be empty
80
+ * @default false
81
+ */
82
+ detached?: boolean;
83
+
84
+ /**
85
+ * Adds control on spawn process stdout
86
+ * @default "utf8"
87
+ */
88
+ stdoutEncoding?: string;
89
+
90
+ /**
91
+ * If detached is true, number of milliseconds to wait to detect an error before exiting JavaCaller run
92
+ * @default 500
93
+ */
94
+ waitForErrorMs?: number;
95
+
96
+ /**
97
+ * You can override cwd of spawn called by JavaCaller runner
98
+ * @default process.cwd()
99
+ */
100
+ cwd?: string;
101
+
102
+ /**
103
+ * List of arguments for JVM only, not the JAR or the class
104
+ */
105
+ javaArgs?: string[];
106
+
107
+ /**
108
+ * No quoting or escaping of arguments is done on Windows. Ignored on Unix.
109
+ * This is set to true automatically when shell is specified and is CMD.
110
+ * @default true
111
+ */
112
+ windowsVerbatimArguments?: boolean;
113
+
114
+ /**
115
+ * If windowless is true, JavaCaller calls javaw instead of java to not create any windows,
116
+ * useful when using detached on Windows. Ignored on Unix.
117
+ * @default false
118
+ */
119
+ windowless?: boolean;
120
+
121
+ /**
122
+ * The number of milliseconds to wait before the Java process will time out. When this occurs,
123
+ * killSignal will ben
124
+ * @default undefined
125
+ */
126
+ timeout?: number;
127
+
128
+ /**
129
+ * If windowless is true, JavaCaller calls javaw instead of java to not create any windows,
130
+ * useful when using detached on Windows. Ignored on Unix.
131
+ * @default "SIGTERM"
132
+ */
133
+ killSignal?: number | NodeJS.Signals;
134
+
135
+ /**
136
+ * On Windows, hide the subprocess console window that would normally be created.
137
+ * This option is ignored on Unix.
138
+ * @default true
139
+ */
140
+ windowsHide?: boolean;
141
+ }
142
+
143
+ /**
144
+ * Result returned by JavaCaller run method
145
+ */
146
+ export interface JavaCallerResult {
147
+ /**
148
+ * Exit status code of the java command
149
+ */
150
+ status: number | null;
151
+
152
+ /**
153
+ * Standard output of the java command
154
+ */
155
+ stdout: string;
156
+
157
+ /**
158
+ * Standard error output of the java command
159
+ */
160
+ stderr: string;
161
+
162
+ /**
163
+ * Child process object (useful when detached is true)
164
+ */
165
+ childJavaProcess?: import('child_process').ChildProcess;
166
+ }
167
+
168
+ /**
169
+ * JavaCaller class for calling Java commands from Node.js
170
+ */
171
+ export class JavaCaller {
172
+ minimumJavaVersion: number;
173
+ maximumJavaVersion?: number;
174
+ javaType?: "jre" | "jdk";
175
+ rootPath: string;
176
+ jar?: string;
177
+ classPath: string | string[];
178
+ useAbsoluteClassPaths: boolean;
179
+ mainClass?: string;
180
+ output: string;
181
+ status: number | null;
182
+ javaSupportDir?: string;
183
+ javaExecutable: string;
184
+ javaExecutableWindowless: string;
185
+ additionalJavaArgs: string[];
186
+ commandJavaArgs: string[];
187
+ javaHome?: string;
188
+ javaBin?: string;
189
+ javaExecutableFromNodeJavaCaller?: string | null;
190
+ prevPath?: string;
191
+ prevJavaHome?: string;
192
+
193
+ /**
194
+ * Creates a JavaCaller instance
195
+ * @param opts - Run options
196
+ */
197
+ constructor(opts: JavaCallerOptions);
198
+
199
+ /**
200
+ * Runs java command of a JavaCaller instance
201
+ * @param userArguments - Java command line arguments
202
+ * @param runOptions - Run options
203
+ * @returns Command result (status, stdout, stderr, childJavaProcess)
204
+ */
205
+ run(userArguments?: string[], runOptions?: JavaCallerRunOptions): Promise<JavaCallerResult>;
206
+ }
207
+
208
+ /**
209
+ * JavaCallerCli class for using java-caller from command line
210
+ */
211
+ export class JavaCallerCli {
212
+ javaCallerOptions: JavaCallerOptions;
213
+
214
+ /**
215
+ * Creates a JavaCallerCli instance
216
+ * @param baseDir - Base directory containing java-caller-config.json
217
+ */
218
+ constructor(baseDir: string);
219
+
220
+ /**
221
+ * Process command line arguments and run java command
222
+ */
223
+ process(): Promise<void>;
224
+ }
@@ -69,12 +69,14 @@ class JavaCaller {
69
69
  * Runs java command of a JavaCaller instance
70
70
  * @param {string[]} [userArguments] - Java command line arguments
71
71
  * @param {object} [runOptions] - Run options
72
- * @param {boolean} [runOptions.detached = false] - If set to true, node will node wait for the java command to be completed. In that case, childJavaProcess property will be returned, but stdout and stderr may be empty
72
+ * @param {boolean} [runOptions.detached = false] - If set to true, node will not wait for the java command to be completed. In that case, childJavaProcess property will be returned, but stdout and stderr may be empty
73
73
  * @param {string} [runOptions.stdoutEncoding = 'utf8'] - Adds control on spawn process stdout
74
74
  * @param {number} [runOptions.waitForErrorMs = 500] - If detached is true, number of milliseconds to wait to detect an error before exiting JavaCaller run
75
75
  * @param {string} [runOptions.cwd = .] - You can override cwd of spawn called by JavaCaller runner
76
76
  * @param {string} [runOptions.javaArgs = []] - You can override cwd of spawn called by JavaCaller runner
77
77
  * @param {string} [runOptions.windowsVerbatimArguments = true] - 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.
78
+ * @param {number} [runOptions.timeout] - In milliseconds the maximum amount of time the process is allowed to run
79
+ * @param {NodeJS.Signals | number} [runOptions.killSignal = "SIGTERM"] - The signal value to be used when the spawned process will be killed by timeout or abort signal.
78
80
  * @return {Promise<{status:number, stdout:string, stderr:string, childJavaProcess:ChildProcess}>} - Command result (status, stdout, stderr, childJavaProcess)
79
81
  */
80
82
  async run(userArguments, runOptions = {}) {
@@ -84,6 +86,7 @@ class JavaCaller {
84
86
  runOptions.stdoutEncoding = typeof runOptions.stdoutEncoding === "undefined" ? "utf8" : runOptions.stdoutEncoding;
85
87
  runOptions.windowsVerbatimArguments = typeof runOptions.windowsVerbatimArguments === "undefined" ? true : runOptions.windowsVerbatimArguments;
86
88
  runOptions.windowless = typeof runOptions.windowless === "undefined" ? false : os.platform() !== "win32" ? false : runOptions.windowless;
89
+ runOptions.killSignal = typeof runOptions.killSignal === "undefined" ? "SIGTERM" : runOptions.killSignal;
87
90
  this.commandJavaArgs = (runOptions.javaArgs || []).concat(this.additionalJavaArgs);
88
91
 
89
92
  let javaExe = runOptions.windowless ? this.javaExecutableWindowless : this.javaExecutable;
@@ -97,10 +100,13 @@ class JavaCaller {
97
100
 
98
101
  const javaExeToUse = this.javaExecutableFromNodeJavaCaller ?? javaExe;
99
102
  const classPathStr = this.buildClasspathStr();
100
- const javaArgs = this.buildArguments(classPathStr, (userArguments || []).concat(this.commandJavaArgs));
103
+ const javaArgs = this.buildArguments(classPathStr, (userArguments || []).concat(this.commandJavaArgs), runOptions.windowsVerbatimArguments);
101
104
  let stdout = "";
102
105
  let stderr = "";
103
106
  let child;
107
+ let timeoutId;
108
+ let killedByTimeout = false;
109
+
104
110
  const prom = new Promise((resolve) => {
105
111
  // Spawn java command line
106
112
  debug(`Java command: ${javaExeToUse} ${javaArgs.join(" ")}`);
@@ -109,7 +115,7 @@ class JavaCaller {
109
115
  cwd: javaExeToUse === "java" || javaExeToUse === "javaw" ? runOptions.cwd : undefined,
110
116
  env: Object.assign({}, process.env),
111
117
  stdio: this.output === "console" ? "inherit" : runOptions.detached ? "ignore" : "pipe",
112
- windowsHide: true,
118
+ windowsHide: runOptions.windowsHide !== undefined ? runOptions.windowsHide : true,
113
119
  windowsVerbatimArguments: runOptions.windowsVerbatimArguments,
114
120
  };
115
121
  if (javaExeToUse.includes(" ")) {
@@ -117,6 +123,19 @@ class JavaCaller {
117
123
  }
118
124
  child = spawn(javaExeToUse, javaArgs, spawnOptions);
119
125
 
126
+ if (runOptions.timeout) {
127
+ timeoutId = setTimeout(() => {
128
+ if (!child.killed) {
129
+ try {
130
+ child.kill(runOptions.killSignal);
131
+ killedByTimeout = true;
132
+ } catch (err) {
133
+ stderr += `Failed to kill process after ${runOptions.timeout}ms: ${err.message}`;
134
+ }
135
+ }
136
+ }, runOptions.timeout);
137
+ }
138
+
120
139
  // Gather stdout and stderr if they must be returned
121
140
  if (spawnOptions.stdio === "pipe") {
122
141
  child.stdout.setEncoding(`${runOptions.stdoutEncoding}`);
@@ -132,12 +151,28 @@ class JavaCaller {
132
151
  child.on("error", (data) => {
133
152
  this.status = 666;
134
153
  stderr += "Java spawn error: " + data;
154
+
155
+ if (timeoutId) {
156
+ clearTimeout(timeoutId);
157
+ }
158
+
135
159
  resolve();
136
160
  });
137
161
 
138
162
  // Catch status code
139
163
  child.on("close", (code) => {
140
- this.status = code;
164
+ if (timeoutId) {
165
+ clearTimeout(timeoutId);
166
+ }
167
+
168
+ if (killedByTimeout) {
169
+ // Process was terminated because of the timeout
170
+ this.status = 666;
171
+ stderr += `Process timed out with ${runOptions.killSignal} after ${runOptions.timeout}ms.`;
172
+ } else {
173
+ this.status = code;
174
+ }
175
+
141
176
  resolve();
142
177
  });
143
178
 
package/package.json CHANGED
@@ -1,14 +1,15 @@
1
1
  {
2
2
  "name": "java-caller",
3
- "version": "4.3.2",
3
+ "version": "4.4.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
+ "types": "./lib/index.d.ts",
6
7
  "files": [
7
8
  "lib/"
8
9
  ],
9
10
  "scripts": {
10
11
  "lint:fix": "eslint **/*.js --fix && prettier --write \"./lib/**/*.{js,jsx,json}\" --tab-width 4 --print-width 150",
11
- "java:compile": "javac -d test/java/dist -source 8 -target 1.8 test/java/src/com/nvuillam/javacaller/JavaCallerTester.java",
12
+ "java:compile": "javac -d test/java/dist --release 8 test/java/src/com/nvuillam/javacaller/JavaCallerTester.java",
12
13
  "java:jar": "cd test/java/dist && jar -cvfm ./../jar/JavaCallerTester.jar ./../jar/manifest/Manifest.txt com/nvuillam/javacaller/*.class && jar -cvfm ./../jar/JavaCallerTesterRunnable.jar ./../jar/manifest-runnable/Manifest.txt com/nvuillam/javacaller/*.class",
13
14
  "test": "mocha \"test/**/*.test.js\"",
14
15
  "test:coverage": "nyc npm run test",
@@ -26,6 +27,7 @@
26
27
  "node",
27
28
  "npm",
28
29
  "javascript",
30
+ "typescript",
29
31
  "class"
30
32
  ],
31
33
  "author": "Nicolas Vuillamy",
@@ -42,10 +44,12 @@
42
44
  },
43
45
  "devDependencies": {
44
46
  "@babel/eslint-parser": "^7.22.15",
47
+ "@types/node": "^25.2.0",
45
48
  "eslint": "^9.0.0",
46
49
  "mocha": "^11.0.0",
47
50
  "nyc": "^17.0.0",
48
51
  "prettier": "^3.1.0",
52
+ "typescript": "^5.9.3",
49
53
  "which": "^6.0.0"
50
54
  },
51
55
  "engines": {