Skip to content

Instantly share code, notes, and snippets.

@misan
Created November 3, 2010 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save misan/661347 to your computer and use it in GitHub Desktop.
Save misan/661347 to your computer and use it in GitHub Desktop.
Embbed Javascript on Java6 code, this code as a calculator on the first parameter
import javax.script.*;
class Eval {
static ScriptEngineManager manager = new ScriptEngineManager();
static ScriptEngine engine= manager.getEngineByName("js");
public static void main(String args[]) {
System.out.println(calc(args[0]));
}
public static String calc(String line) {
try {
engine.eval("i="+line); // calculate expression
return(engine.get("i").toString());
}
catch(ScriptException se) {return("ERROR"); }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment