简体   繁体   中英

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

I wrote code in app.js to run the jar file with some parameters, which will run the command like this:

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

How can I change the path where the result of the Java file is generated? Ideally into \\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 )

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.

I believe that adding the node.js exec option cwd to the directory where you would like the output would do the trick.

Look further here: child_process.exec(command[, options], callback)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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