3

Is it possible to run a Java file from a node application, and get the response of it? For example something as simple as:

package com.app;

public class App {
    public static void main(String[] args) {
        System.out.println('Hello World');
    }
}

And I want to get the response Hello World

4
  • 1
    Your node app would have to compile that class, then sure, you can capture the standard output just like any other process... Commented Oct 25, 2016 at 20:25
  • 1
    I am curious to see your use case here, if you don't mind Commented Oct 25, 2016 at 20:37
  • There is another question and answer like this in here. stackoverflow.com/questions/5775088/… Commented Oct 25, 2016 at 20:51
  • Run the Java file with .spawn() or .exec() like you would run any external process and then examine stdout. Commented Oct 25, 2016 at 21:26

2 Answers 2

5
  1. Compile you java file
  2. execute it by node? in this way

let childProcess = require('child_process').spawn(
      'java', ['-jar', 'yureJavaFile.jar']
    );

childProcess.stdout.on('data', function(data) {
    console.log(data);
});

childProcess.stderr.on("data", function (data) {
    console.log(data);
});

Sign up to request clarification or add additional context in comments.

1 Comment

Hello Sir i too have same problem but in my case on running of jar it accepts multiple inputs from jar ...how to get this from node
1

If you want to do more complex interaction between java and node you may want to look at node-java. It allows you to create and handle Java objects in a node application and call Java functions in external java class files or jars.

var ArrayList = java.import('java.util.ArrayList');
var list3 = new ArrayList();
list3.addSync('item1');
list3.equalsSync(list1); // true

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.