Use Java scripting engine (JDK 1.6)
import java.util.List; import javax.script.ScriptEngine; import javax.script.ScriptEngineFactory; import javax.script.ScriptEngineManager; public class Main { public static void main(String[] args)throws Exception { ScriptEngineManager mgr = new ScriptEngineManager(); List<ScriptEngineFactory> engines = mgr.getEngineFactories(); for (ScriptEngineFactory engine : engines) { System.out.println(engine.getEngineName()); for (String n : engine.getNames()) { System.out.println("Short name : " + n); } } ScriptEngine engine = mgr.getEngineByName("JavaScript"); String myJSCode = "function myFunction(){return (4+2);}myFunction();"; System.out.println(engine.eval(myJSCode)); } }