0

I have a form data sending strings and ints to my Express server and I need to use my Java backend for calculations and response to the frontend, can I do this directly from the Express server or is there other steps I need to involve?

1
  • GraalVM allows you to use java in javascript directly, if you don't wish to use exec like other answers suggest. Commented Mar 31, 2020 at 15:20

3 Answers 3

1

You can execute java commands from nodejs. You can run exec commands via expressjs router. For the best case scenario, I would create another API with Java and make request to that endpoint (microservices). But if you don't want to do that you can try this code sample;

const express = require('express')
const app = express()
const port = 3000
const exec = require('child_process').exec
app.get('/', (req, res) => {
  const child = exec('/usr/bin/java ~/example.jar', => (error, stdout, stderr) {
    if (err) {
        console.error(err);
        res.json({error: err, status: 500, errorOutput: stderr})
        return
    }
    // it is important to have json structure in your output or you need to create a logic which parse the output
    res.json(stdout)
  })
})

app.listen(port, () => console.log(`Example app listening on port ${port}!`)) 
Sign up to request clarification or add additional context in comments.

Comments

1

You can use Node.js 'exec' to call an external Java program like this:

Javascript program

const exec = require('child_process').exec;

// Number 7 is a command line argument to pass to the Java program
exec('java MyJavaApplication 7', function callback(error, stdout, stderr){
    console.log(stdout);
});

Java program

public class MyJavaApplication {
    public static void main(String[] args) {
        int input = Integer.parseInt(args[0]);
        int output = calculate(input);
        System.out.println(Integer.toString(output));
    }

    private static int calculate(int input) {
        // Do some complex calculation
        return input * input;
    }
}

On Node.js you can capture whatever the Java program wrote to the standard output. Depending on how complex your input is you might want to pass a file name as an argument to the Java program. That file could have the input content in a JSON format, for instance.

Comments

0

You can use a library called a "bridgeService". This exposes java methods as REST objects. You can deploy it locally or on a server.

For a method like "a.b.c.d.E.f(String argument)"

The payload will be something like:

{
  "callContent": {
    "<ID>": {
      "class": "a.b.c.d.E",
      "method": "f",
      "args": [
        "argument"
      ]
    }
  }
}

For more information on the bridgeService you can refer to https://github.com/adobe/bridgeService

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.