-1
Is there a way in java (Springboot app) 
- to invoke NodeJs script file, 
- pass java object to the script and modify that object in the js script. 
- And return back the modified object    

for example as below.

class Services
{
    Address address = new Address(....);

    /*
    invoke NodeJs script and pass address object

    in the js script modify address object
    var address = getAddress()
    modify address object
    return address object back to java calling class
    setResultObject(address) or return address;
    */

    address = invokeJS("nameOfScriptWithLocation", address);
    System.out.println("modified address" + address.toString()
}
1

1 Answer 1

1

I know there are two ways to call node.js code in java.
First if you have the node js environment in your target server,you can use the command line to do it.
Follow is a simple example if that you hava a script named "script.js"

ProcessBuilder pb = new ProcessBuilder("node", "script.js","name=jack");
Process process = pb.start();

InputStream stdout = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));

String line;
while ((line = reader.readLine()) != null) {
    System.out.println(line);
}

The second method is to use some tripartite class library to invoke the js code. Just like graal js, J2V8,etc.

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

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.