Can't save a reference to a Java method, then call it later

Skip to first unread message

Oct 27, 2013, 12:28:59 PM10/27/13
I'm trying to save a reference to a Java object's method in a var, then call it later, but it fails with

Java method "run" was invoked with [object Object] as "this" value that can not be converted to Java type java.lang.Runnable.

Here's the code:

Context cx = Context.enter();
Scriptable scope = cx.initStandardObjects();
scope.put("foo", scope, Context.toObject(new Runnable() {
@Override public void run() {
}, scope));
cx.evaluateString(scope, "; var bar =; bar();", "", 1, null);

I understand why this would fail; Rhino either didn't track Java's this when saving the reference or is having problems converting it. What's the right approach to accomplish this?

Reply all
Reply to author
0 new messages