Thursday, April 27, 2017

Call java program from Node.js application

Leave a Comment

From what I read, there are a couple of ways to run java files in a node.js application. One way is to spawn a child process: (the java code is packaged with dependencies in an executable jar.)

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);         }     }); 

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

In a production environment, when I want my node.js (Express) server to call a java program (it just saves an image to the local directory), please advise me on which would be the better way to accomplish this (in terms of best practices). Also, there is a long list of arguments that I need to pass to the main class and doing that on the command line is a bit of a struggle. Should I make the java program read from an input file instead?

4 Answers

Answers 1

1) If you use exec, you will run an entire program, whereas if you use a JNI interface, you'll be able to directly interact with the libraries and classes in the jar and do things like call a single function or create an instance of a class. However, if you don't need anything like that, I think using exec is far simpler and will also run faster. Sounds like you just want to run the Java application as a standalone process, and just log whether the application finished successfully or with errors. I'd say it's probably better to just use exec for that. Executing a child process this way is also far better for debugging, debugging JNI errors can be very difficult sometimes.

2) As for whether or not to read arguments from a file, yes, it's usually better to read from some sort of file as opposed to passing in arguments directly. It's less prone to human error (ie. typing in arguments every time), and far more configurable. If someone like a QA engineer only needs to edit a config file to swap out options, they don't need to understand your entire codebase to test it. Personally I use config files for every Java program I write.

Answers 2

You can use deployment toolkit and run the jar through jnlp. https://docs.oracle.com/javase/8/docs/technotes/guides/deploy/deployment_toolkit.html Advantage of running jars through jnlp is the ability to pass parameters from javascript to your jar. In this way you can dynamically customize your java program.

Answers 3

For this kind of problem you'd want to approach it in the following way:

  • Is there a decent way to run processes with arguments in my language/framework
  • Is there a decent way to deal with the programs output?

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

A decent way to deal with output is to use a listener/event based model. This way, you respond appropriately to the events instead of having if blocks for stderr and stdout. Again, this makes things readable and let's you handle output in a more maintainable manner.

If you go a bit further into this, you will also have to solve a problem of how to inject environment variables into your target program. As an example, you might want to run the java with a debugger or with less memory in the future, so your solution would also need to cater for this.

This is just one way of solving this kind of problem. If node is your platform, then have a look at Child Process which supports all of these techniques.

Answers 4

Try this into nodejs file:
https://www.npmjs.com/package/java
Or this into html response file:
<object width="400" height="400" data="helloworld.swf"></object>

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment