2012年1月4日 星期三

BeanShell - Lightweight Scripting for Java

Link: http://www.beanshell.org/

Small, free, embeddable, source level Java interpreter with object based scripting language features, written in Java. [Open Source, LGPL]

Example:
import java.util.Date;
import bsh.Interpreter;

public class Main {

    public static void main(String[] args) throws Exception {
        Interpreter i = new Interpreter(); // Construct an interpreter
        
        {
            i.set("date", new Date()); // Set variable 
            System.out.println((Date)i.get("date")); // Retrieve a variable
        }
        
        {
            i.set("foo", 5); // Set variable
            i.eval("bar = foo * 10"); // Evaluate a statement and get the result    
            System.out.println(i.get("bar"));
        }
    }
    
}

沒有留言:

張貼留言