1

I have 2 query , first query collect data from my job table and second query collect data from my vehicle table then both data are stored in results array. How to pass this results array into my child process as argument for jar execution? And is there any size limitation for arguments in jar execution because there is a lot of data in my job table and vehicle table. If there is size limitation for argument, what is the best way to run a jar file with large argument size?

var express = require('express');
var router = express.Router();

router.post('/', async function(req, res, next) {

    var date = req.param('date');
    var joblist;
    var vehiclelist;

    var promises = [];
    promises.push(query1(date));
    promises.push(query2());

    var results = await Promise.all(promises);
    console.log(results[0]); // query1
    console.log(results[1]); // query2

    function query1(date) {
        return new Promise((resolve, reject) => {
            res.locals.connection.query('SELECT * FROM job WHERE date = ?',[date] , function (error, results, fields) {
                if (error) {
                    reject(error);
                } else {
                    resolve(results);
                }
            });
        });
    }

    function query2() {
        return new Promise((resolve, reject) => {
            res.locals.connection.query('SELECT * FROM vehicle' , function (error, results, fields) {
                if (error) {
                    reject(error);
                } else {
                    resolve(results);
                }
            });
        });
    }

    var exec = require('child_process').exec, child;
child = exec('java -jar /home/java/testsize.jar',
  function (error, stdout, stderr){
    console.log('stdout: ' + JSON.stringify(stdout));
    //console.log('stderr: ' + stderr);
    res.send(joblist+"  "+stderr);
    if(error !== null){
      console.log('exec error: ' + error);
    }
});

});

module.exports = router;
2
  • what is test in the exec ? Commented May 2, 2019 at 3:53
  • @saurabh just an argument I created for testing,forgot to remove just now Commented May 2, 2019 at 4:49

1 Answer 1

1

I am assuming the jar file excepts command line arguments.

Syntax to pass command line arguments in java is: java -jar javafilename arg1 arg2 ....

so to pass the command line arguments in your code:

/*variable 'store the results of two array in string'*/
const cmdArgs = [...results[0], results[1]].toString().replace(/[, ]+/g, " ");
/* variable 'command line code' */
const cmdCode = `java -jar /home/java/testsize.jar ${cmdArgs}`;
 var exec = require('child_process').exec, child;
child = exec(cmdCode,
  function (error, stdout, stderr){
    console.log('stdout: ' + JSON.stringify(stdout));
    //console.log('stderr: ' + stderr);
    res.send(joblist+"  "+stderr);
    if(error !== null){
      console.log('exec error: ' + error);
    }
});

If the jar file accepts the results as inputs you can simply pass them by pipe '|'.

Example:

/*variable 'store the results to two array in string'*/
const cmdArgs = [...results[0], results[1]].toString().replace(/[, ]+/g, " ");
/* variable 'command line code' */
const cmdCode = `${cmdArgs} | java -jar /home/java/testsize.jar `;
 var exec = require('child_process').exec, child;
child = exec(cmdCode,
  function (error, stdout, stderr){
    console.log('stdout: ' + JSON.stringify(stdout));
    //console.log('stderr: ' + stderr);
    res.send(joblist+"  "+stderr);
    if(error !== null){
      console.log('exec error: ' + error);
    }
});

As for the size limitation for argument.

Didn't find anything about that but, different OS has a maximum command-line length limit.

If the size of the arguments is large enough to exceed the command-line length limit, you can go for the Input stream of java to accept the data as input (need to change the code java(jar file) code for that to accept data input).

you can store the result in a temp file and then pass them to the jar pass through pipe.

Example:

/*require fs to write into file*/
var fs = require("fs");

/*variable 'store the results to two array in string'*/
const cmdArgs = [...results[0], results[1]].toString().replace(/[, ]+/g, " ");
/*set file name*/
const tempFile = 'temp' + new Date().valueOf() + '.txt';
fs.writeFileSync(tempFile, cmdArgs);
/* variable 'command line code' */
const cmdCode = `cat > ${tempFile} | java -jar /home/java/testsize.jar `;
 var exec = require('child_process').exec, child;
child = exec(cmdCode,
  function (error, stdout, stderr){
    /*delete the file*/
    fs.unlinkSync(tempFile);
    console.log('stdout: ' + JSON.stringify(stdout));
    //console.log('stderr: ' + stderr);
    res.send(joblist+"  "+stderr);
    if(error !== null){
      console.log('exec error: ' + error);
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

if store result in a temp text file. Will it be any issue when this node server receive multiple request? For example, 2 users send request to run this process at the same time, but there is only 1 temp.txt. Will the temp file get overwrite when user 1 is reading the temp file?
you can solve that by assigning a unique name to the temp file and storing them in a variable. you can also write a code to clean up the temp file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.