Skip to content

Commit 57f7af4

Browse files
committed
added java_process module to allow start java app from node and communicate through stdin and stderr
0 parents  commit 57f7af4

19 files changed

Lines changed: 2982 additions & 0 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

java/EchoMessage.class

901 Bytes
Binary file not shown.

java/EchoMessage.jar

1.02 KB
Binary file not shown.

java/EchoMessage.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
import java.io.BufferedReader;
3+
import java.io.InputStreamReader;
4+
5+
class EchoMessage {
6+
public static void main(String[] args) throws Exception{
7+
System.out.println("READY");
8+
while(true) {
9+
BufferedReader buffer=new BufferedReader(new InputStreamReader(System.in));
10+
String line=buffer.readLine();
11+
System.out.println("Echo: " + line);
12+
}
13+
14+
}
15+
}

java/MANIFEST.MF

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Manifest-Version: 1.0
2+
ImplementationVersion: 0.0.1
3+
Main-Class: EchoMessage
4+

package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "node-java-ipc",
3+
"version": "1.0.0",
4+
"description": "Node to Java Ipc using stdin, stdout and stderr, using a small and simple protocol allowing node to interact with java",
5+
"main": "js/index.js",
6+
"scripts": {
7+
"build": "tsc",
8+
"test": "tsc && node src/js/test.js"
9+
},
10+
"author": "Abner Oliveira",
11+
"license": "MIT",
12+
"dependencies": {
13+
"shelljs": "^0.6.0"
14+
}
15+
}

src/js/index.d.ts

Whitespace-only changes.

src/js/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var shelljs = require("shelljs");
2+
var javaLocation = shelljs.which("java");
3+
if (!javaLocation) {
4+
throw new Error("Java executable not found. Check if java is installed and present in PATH environment variable");
5+
}
6+
var result = shelljs.exec("java -version");
7+
var javaVersion = null;
8+
if (result.code === 0) {
9+
console.log("Java version result: ", result);
10+
console.log(result.output);
11+
var match = result.stderr.match(/(?!\.)(\d+(\.\d+)+)(?![\d\.])/);
12+
if (match.length > 0) {
13+
javaVersion = match[0];
14+
}
15+
console.log("JAVA VERSION: ", javaVersion);
16+
}

src/js/java_process.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export declare function start(jarPath: string, args?: string[]): {
2+
javaLocation: string;
3+
javaVersion: String;
4+
process: any;
5+
};

src/js/java_process.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var shelljs = require("shelljs");
2+
function start(jarPath, args) {
3+
var javaLocation = shelljs.which("java");
4+
if (!javaLocation) {
5+
throw new Error("Java executable not found. Check if java is installed and present in PATH environment variable");
6+
}
7+
var result = shelljs.exec("java -version");
8+
var javaVersion = null;
9+
if (result.code === 0) {
10+
var match = result.stderr.match(/(?!\.)(\d+(\.\d+)+)(?![\d\.])/);
11+
if (match.length > 0) {
12+
javaVersion = match[0];
13+
}
14+
console.log("JAVA VERSION: ", javaVersion);
15+
}
16+
else {
17+
throw new Error("Error extracting java version");
18+
}
19+
var spawn = require("child_process").spawn;
20+
var argsJava = ["-jar", jarPath];
21+
if (args) {
22+
argsJava = argsJava.concat(args);
23+
}
24+
var child = spawn("java", argsJava);
25+
return {
26+
javaLocation: javaLocation,
27+
javaVersion: javaVersion,
28+
process: child
29+
};
30+
}
31+
exports.start = start;

0 commit comments

Comments
 (0)