Call java program from Node.js application

后端 未结 5 1494
萌比男神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: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.

提交回复
热议问题