Home

Calling scripts from Java

Create an interpreter and source a script file, returning a value:

CallScript1.java - Invoke a script from Java
import bsh.*;

public class CallScript1 {
    public static void main( String [] args ) throws EvalError {
        Object result = new bsh.Interpreter().source("myscript.bsh");
    }
}

This time we'll handle the two types of EvalError that can be thrown:

CallScript2.java - Invoke a script from Java, handling errors
import bsh.*;

public class CallScript2 {
    public static void main( String [] args ) throws Exception {
        try {
            Object obj = new bsh.Interpreter().source("myscript.bsh");
        } catch ( TargetError e ) {
            System.out.println(
                "The script or code called by the script threw an exception: "
                + e.getTarget() );
        } catch ( EvalError e2 )    {
            System.out.println(
                "There was an error in evaluating the script:" + e2 );
        }
    }
}