Call java program from Node.js application

后端 未结 5 1488
萌比男神i
萌比男神i 2020-12-16 03:37

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 pac

相关标签:
5条回答
  • 2020-12-16 03:43

    We can run the whole java project by making .jar file of it and run it using the command in the shell and run that shell file. In order to run java code from nodejs project as we know project could be a mix of java, js modules. Call exec() function in node to create a child process to execute the shell file having a command to run .sh file and can also pass some argument in it from use.eg;

    let fileName = 'someFile.txt';
    let userName = 'Charlie Angle'; 
    exec(`sh run.sh --context_param
    paramFilePath="./storage/${fileName}" --context_param userName="${userName}"`, (error, stdout, stderr) => {// Some code based on execution of above command})
    
    0 讨论(0)
  • 2020-12-16 03:46

    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.

    0 讨论(0)
  • 2020-12-16 03:55

    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.

    0 讨论(0)
  • 2020-12-16 04:05

    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.

    0 讨论(0)
  • 2020-12-16 04:05

    You can simply call a java command , with classpath & arguments, using module node-java-caller, it embeds the call to spawn and will also automatically install java if not present on the system

    https://github.com/nvuillam/node-java-caller

    0 讨论(0)
提交回复
热议问题