Running Java from a Script with Nashorn

208 Views Asked by At

I am trying to use Nashorn as a scripting language and I would like to start a Java program from that a script that is run with Nashorn.

I can sucsessfully run the following script with Nashorn:

#!/usr/bin/jjs -fv
$EXEC("ls -l")

However the following script seems to do nothing:

#!/usr/bin/jjs -fv
$EXEC("java -version")

On the commandline java -version of course works...

What might be my problem?

1

There are 1 best solutions below

1
On

Nashorn can call methods from Java classes. So you can just use it to evoke the main-method of the main class of the program you want to start. A precondition is that you run jjs with the the parameter -cp path/to/java/classes/of/program so it knows about the classes of said program.