从Node.js应用程序调用Java程序 [英] Call java program from Node.js application

查看:110
本文介绍了从Node.js应用程序调用Java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,有两种方法可以在 node.js 应用程序中运行 java 文件.一种方法是产生一个子进程:(Java代码与依赖关系打包在可执行文件 jar 中.)

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

另一种方法是使用 java-npm 模块(链接),在 JNI 上进行包装(这将使我创建 objects ,设置并获取 attributes ,运行 methods ).

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).

在生产环境中,当我希望我的 node.js ( Express )服务器调用 java 程序时(它只保存一个图片到本地目录),请告诉我哪种方法可以更好地完成此操作(按照最佳做法).另外,我需要传递一长串的 arguments main 类,并且在命令行上这样做有点麻烦.我应该改为从输入文件中读取 java 程序吗?

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?

推荐答案

1)如果使用 exec ,则将运行整个程序,而如果使用JNI接口,则将能够直接与jar中的库和类进行交互,并执行诸如调用单个函数或创建类的实例之类的操作.但是,如果您不需要这样的东西,我认为使用 exec 会更简单,并且运行速度也会更快.听起来您只是想将Java应用程序作为一个独立的进程运行,并且只记录该应用程序是否成功完成或出现错误.我想说,最好使用 exec .以这种方式执行子进程对于调试也要好得多,有时调试JNI错误可能非常困难.

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)至于是否从文件中读取参数,是的,通常最好是从某种文件中读取,而不是直接传递参数.它不太容易出现人为错误(即每次都键入参数),并且更具可配置性.如果像QA工程师这样的人只需要编辑配置文件以换出选项,那么他们就不需要了解整个代码库即可对其进行测试.我个人为每个编写的Java程序都使用配置文件.

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.

这篇关于从Node.js应用程序调用Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆