Java | Sunday, April 30, 2006

JSR-223 script engine for the Java language

I mentioned about the http://scripting.dev.java.net project here and here

I've added script engine for the Java programming language. This engine uses Java Compiler API (JSR-199) to compile the "script" and then uses a memory ClassLoader to load the generated class(es). I find being able to "eval" Java code is kind of cool! If you also like it, you may want to checkout Java script engine from java.net and try out java.sh (or java.bat) in the bin directory (which is a wrapper over jrunscript)

Sample code that uses Java script engine:


import javax.script.\*;
class Main {
public static void main(String[] args) throws ScriptException {
ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine e = m.getEngineByName("java");
e.eval("class t { " +
" public static void main(String[] args) { " +
" System.out.println(\\" hello from eval\\"); " +
" }" +
"}");
}
}

You can compile the above code and run by putting java-engine.jar and tools.jar in the classpath -- or may be you can just "eval" it using jrunscript :-)
jrunscript  -cp java-engine.jar -l java -f Main.java

Join the discussion

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha
 

Visit the Oracle Blog

 

Contact Us

Oracle

Integrated Cloud Applications & Platform Services