Call java program from Node.js application

From what I read, there are several ways to run java files in a node.js application. One way is to create a child process: (Java code is packed with dependencies in the jar executable.)

 var exec = require('child_process').exec, child; child = exec('java -jar file.jar arg1 arg2', function (error, stdout, stderr){ console.log('stdout: ' + stdout); console.log('stderr: ' + stderr); if(error !== null){ console.log('exec error: ' + error); } }); 

Another way is to use the java - npm module java - npm ( link ), a wrapper over JNI (this will allow me to create objects , set and get attributes , run methods ).

In a production environment, when I want my node.js ( Express ) server to call the java program (it just saves the image in a local directory), please tell me which one will be the best way to achieve this (from an advanced point of view experience). In addition, there is a long list of arguments that I need to pass to the main class, and doing this on the command line is a battle. Should I make a java program read from the input file?

+8
java express
source share
4 answers

1) If you use exec , you will run the whole program, whereas if you use the JNI interface, you can directly interact with libraries and classes in the bank and do things such as calling one or creating an instance of the class. However, if you do not need anything like this, I think using exec much simpler and will also work faster. It looks like you just want to run the Java application as a standalone process and just register whether the application completed successfully or with errors. I would say that it is probably better to just use exec for this. Running the child process this way is also much better for debugging, debugging JNI errors can sometimes be very complicated.

2) Regarding the reading or absence of arguments from a file, yes, it is usually better to read from some file rather than passing arguments directly. It is less prone to human error (i.e., it picks up arguments every time) and is much more customizable. If someone, as a QA engineer, only needs to edit the configuration file to replace the options, they do not need to understand their entire code base to verify it. Personally, I use configuration files for every Java program that I write.

+4
source share

You can use the deployment toolkit and run jar through jnlp. https://docs.oracle.com/javase/8/docs/technotes/guides/deploy/deployment_toolkit.html The advantage of running jars-jars is the ability to transfer parameters from javascript to your bank. This way you can dynamically tune your java program.

0
source share

For this "strong" problem, you need to approach it as follows:

  • Is there a decent way to start processes with arguments in my language / framework
  • Is there a decent way to handle output programs?

From experience, a decent way to handle arguments in a process is to pass them as an (string) array. This is advantageous in that you do not need to resort to unnecessary string interpolation and manipulation. It is also more readable, which is a plus in this task.

A decent way to process the output is to use a model based on listeners / events. This way you respond appropriately to events, not blocks for stderr and stdout. Again, this makes reading understandable and allows you to process the output in a more convenient way.

If you delve deeper into this, you will also have to solve the problem of how to embed environment variables in the target program . For example, you can run java with a debugger or with less memory in the future, so your solution would also have to satisfy this.

This is just one way to solve this problem . If node is your platform, take a look at Child Process , which supports all of these methods.

0
source share

Try this in the nodejs file:
https://www.npmjs.com/package/java
Or this is in the html response file:
<object width="400" height="400" data="helloworld.swf"></object> inconvenient that it reads only .swf files ... ... except that you add the java type:

0
source share

All Articles