On Aug 8, 2:35 pm, Jonathan Scott <[email protected]> wrote:
> I guess I'm missing something obvious from the documentation, but I
> can't figure out how to let the javascript access a Java object. I
> guess the error means it can access it but for some reason can't access
> the method. Can anyone correct me?
>
> import org.mozilla.javascript.Context;
> import org.mozilla.javascript.ScriptableObject;
>
> public class RhinoTest {
> public static void main(String[] args) {
> Context context = Context.enter();
> ScriptableObject scope = context.initStandardObjects();
> ScriptableObject.putProperty(scope, "out", Context.javaToJS(new
> LiveSite(), scope));
> context.evaluateString(scope, "out.print('a');", "asdads", 0, null);
> }
>
> }
>
> class LiveSite {
> public void print(String a) {
> System.out.println(a);
> }
>
> }
>
> gives me the following exception:
>
> Exception in thread "main" org.mozilla.javascript.EcmaError: TypeError:
> Cannot find function print in object lives...@e70e30.
> at
> org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3654)
> at
>
> org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3632)
> at
> org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3660)
> at
> org.mozilla.javascript.ScriptRuntime.typeError2(ScriptRuntime.java:3679)
> at
> org.mozilla.javascript.ScriptRuntime.notFunctionError(ScriptRuntime.java:3743)
> at
>
> org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThisHelper(ScriptRuntime.java:2247)
> at
>
> org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime.java:2214)
> at
>
> org.mozilla.javascript.gen.c1._c0(asdads:0)
> at org.mozilla.javascript.gen.c1.call(asdads)
> at
> org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:398)
> at
> org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3065)
> at org.mozilla.javascript.gen.c1.call(asdads)
> at org.mozilla.javascript.gen.c1.exec(asdads)
> at org.mozilla.javascript.Context.evaluateString(Context.java:1104)
> at RhinoTest.main(RhinoTest.java:9)
I got the same error you did (well, I had to add the static qualifier
on the LiveSite class to get it to compile).
I then added the public qualifier as well, and it worked fine.
public static class LiveSite {
public void print(String a) {
System.out.println(a);
}
}
Jeremy
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino