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

查看:62
本文介绍了从 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 模块(linka>),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天全站免登陆