invoke beanshell methods from java

Hello

I'm learning beanshell and using it to write scripts that are ran in JDK 6.

My question is how to invok beanshell methods from java source codes.

My codes is:

import javax.script.*;

public class InvokeFunctions {

public static void main (String[] args)throws ScriptException, NoSuchMethodException

{

ScriptEngineManager sem = new ScriptEngineManager();

ScriptEngine bshEngine = sem.getEngineByName("beanshell");

String script = "public void sayHello()"+"{print (\"sayHello() is a method in bsh script\");}";

bshEngine.eval(script);

Invocable inbshEngine = (Invocable)bshEngine;

inbshEngine.invokeFunction("sayHello");

}

}

I defined a method "sayHello()" using beanshell, but i just can't invoke it in Java.

I got an error msg said:

Exception in thread "main" java.lang.IllegalAccessError: tried to access method bsh.NameSpace.getThis(Lbsh/Interpreter;)Lbsh/This; from class bsh.engine.BshScriptEngine

Any one has any idea about it?

Thanks

[1073 byte] By [Winptora] at [2007-11-26 15:05:32]
# 1

Look at the Javadoc documentation of IllegalAccessError. It says:

Thrown if an application attempts to access or modify a field, or to call a method that it does not have access to.

Normally, this error is caught by the compiler; this error can only occur at run time if the definition of a class has incompatibly changed.

Maybe recompiling all your sources (make sure you delete all existing *.class files) will help?

jesperdja at 2007-7-8 8:55:21 > top of Java-index,Java Essentials,Java Programming...