0

I want to run a JavaScript code snippet which returns boolean, from java.

Currently I am using ProcessBuilder to run the JavaScript program with the command jjs filename.

I am able to perform the operations and getting the output from getOutputStream properly.

But my requirement is to return boolean from the script, when I make the similar changes in the script it gives compile time error invalid return statement, which is obvious.

when I use

var a=5; var b=6; print(a+b);

I am getting a output 11. But I want to execute a script like

var a=5; 
if(a>0) 
    return true; 
else 
    return false;

I get errorStream like Invalid return statement Expected eof but found } } ^

  • 1
    In order to get help, you need to provide some code that shows what you have tried. – Just another Java programmer Mar 21 '22 at 07:13
  • Is the boolean part of the standard output from the js script? Or is it some success/failure state that's given in some other way? If yes, how is it given? – Federico klez Culloca Mar 21 '22 at 07:24
  • when I use var a=5; var b=6; print(a+b); I am getting a output 11. But I want to execute a script like var a=5; if(a>0) return true; else return false; I get errorStream like Invalid return statement Expected eof but found } } ^ – Sadanand Kolhe Mar 21 '22 at 08:11
  • Hello Federico, I'm not quite understanding your question. I've updated the description with an example please have a look at it. – Sadanand Kolhe Mar 21 '22 at 08:18
  • Please show exactly what you have, in a way we can run. We need to see the ProcessBuilder, the full js file, the expected output and the actual output. Plus full errors when they happen – tim_yates Mar 21 '22 at 17:51

1 Answers1

0

The getInputStream method of the ProcessBuilder class gives you a stream from which you can read bytes. You have to convert the bytes to String and then parse to boolean.

stepanian
  • 11,373
  • 8
  • 43
  • 63
  • I am getting a compile time error when do the changes in the code for returning boolean. As I am not creating any method to write the code in it. Please mention if I have to use any other way to solve this issue. – Sadanand Kolhe Mar 21 '22 at 08:15