With the JavaScript API through Rhino, you can simply compile the script using the Compilable interface. For example:
public class CompileScript { public static void main(String[] args) throws ScriptException { ScriptEngineManager engineManager = new ScriptEngineManager(); ScriptEngine scriptEngine = engineManager.getEngineByName("js");
However, the benefits of this will be displayed when you run the script several times. This basically reduces the overhead of re-interpreting each time. From CompiledScript javadoc:
Extended classes that store compilation results. State can be stored as Java classes, Java class files, or scripted opcode codes. The script can be executed repeatedly without reprocessing.
Anyway, I think you should take a look at the Rhino JavaScript Compiler . It "translates JavaScript source code into Java class files."
And there is an implementation of V8 Java. Check out jav8 .
Darkbyte
source share