I am not saying that it is the best solution but it is one of the available options. I just want to spread awareness and one reason this is how Java runs javascript because it has an embedded JavaScript runtime already for a long time. First there was Rhino, and now, Java SE 8 shipped with a new engine called Nashorn, which is based on JSR 292 and invokedynamic. It provides better compliance with the ECMA normalized JavaScript specification and better runtime performance through invokedynamic-bound call sites. It can be used to run JavaScript programs from the command line. To do so, builds of Oracle’s JDK or OpenJDK include a command-line tool called jjs. It can be found in the bin/ folder of a JDK installation along with the well-known java, javac, or jar tools.
The jjs tool accepts a list of JavaScript source code files as arguments. Consider the following hello.js file:
var hello = function() {
print("Hello Nashorn!");
};
hello();
Evaluating it is as simple as this:
$ jjs hello.js
Hello Nashorn!
$
For more detail you can refer official documentation http://www.oracle.com/technetwork/articles/java/jf14-nashorn-2126515.html