« November 2009
SunMonTueWedThuFriSat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
     
       
Today
XML

Blog::Navigation

GetJava Download Button
Get the Source
Personal Blog

Blog::Referers

Today's Page Hits: 886

Powered by Roller Weblogger.
« Turning on/off and... | Main | Meet you @ JavaOne... »
20060501 Monday May 01, 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



( May 01 2006, 10:09:55 AM IST ) Permalink del.icio.us | furl | simpy | slashdot | technorati | digg

Comments:

Post a Comment:

Comments are closed for this entry.
Copyright (C) 2005, A. Sundararajan's Weblog