我需要在我的代码中调用一些java代码。我使用 BeanShell 来实现。 所以,我可以这样做:
public void testInterpreter() {
Interpreter i = new Interpreter();
i.eval("System.out.println("test1"));
}
但是如果我想在解释器中调用其他方法怎么办?我想要这样的东西:
public void testInterpreter() {
Interpreter i = new Interpreter();
i.eval("testMethod()");
}
public void testMethod() {
System.out.println("test2")
}
但我收到错误“找不到命令”
最佳答案
将类的实例设置为解释器上的变量:
i.set("instance", this);
i.eval("instance.testMethod()");
关于java - 借助 BeanShell 调用嵌入代码中的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34740496/