简体   繁体   English

从root的NodeJS输出文件中调用Java

[英]Calling Java from NodeJS outputs file in root

For a project structure like this: 对于这样的项目结构:

/myfolder/app/components/owl2vowl.jar
/myfolder/app/uploads/ontology.owl
/myfolder/app.js

I am using OWL2VOWL to convert an ontology into a JSON 我正在使用OWL2VOWL将本体转换为JSON

I wrote code in app.js to run the jar file with some parameters, which will run the command like this: 我在app.js编写了代码,以使用一些参数运行jar文件,这将运行如下命令:

java -jar e:\myfolder\app\components\owl2vowl.jar -file e:\myfolder\app\uploads\ontology.owl 

Here is the code: 这是代码:

var exec = require('child_process').exec, child;
child = exec('java -jar ' +  __dirname + '/components/owl2vowl.jar' + ' -file ' + syncPath ,
function (error, stdout, stderr){
  if(error !== null)
    console.log('There was an error parsing the ontology' + error);
  else           
    console.log('Succes parsing the ontology');

where 哪里

syncPath = e:\myfolder\app\uploads\ontology.owl 

The problem is that the result is generated in folder myfolder generating the file ontology.json 问题是结果是在文件夹myfolder生成的,从而生成文件ontology.json

How can I change the path where the result of the Java file is generated? 如何更改生成Java文件结果的路径? Ideally into \\app\\uploads ? 理想情况下进入\\app\\uploads

/myfolder/app/components/owl2vowl.jar
/myfolder/app/uploads/ontology.owl
/myfolder/app.js
/myfolder/ontology.json

Edit - Added solution 编辑-添加了解决方案

the solution a suggested by javabeangrinder is to add a cwd option (in the following, the result will be outputted in the folder /components ) javabeangrinder建议的解决方案是添加一个cwd选项(在下面,结果将输出到文件夹/components

var options = { encoding: 'utf8',
                              timeout: 0,
                              maxBuffer: 200*1024,
                              killSignal: 'SIGTERM',
                              cwd:  __dirname + '/components/',
                              env: null }

child = exec('java -jar ' +  __dirname + '/components/owl2vowl.jar' + ' -iri ' + '"'+body.url+'"'+'  -echo' , options,
                              function (error, stdout, stderr){})

The problem is not the java code although it could be fixed changing it to accept an output folder as suggested earlier. 问题不是Java代码,尽管可以将其更改为接受它,如先前建议的那样将其更改为接受输出文件夹。

I believe that adding the node.js exec option cwd to the directory where you would like the output would do the trick. 我相信,将node.js exec选项cwd添加到您希望输出的目录即可解决问题。

Look further here: child_process.exec(command[, options], callback) 在此处进一步查看: child_process.exec(command [,options],callback)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM