File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes Need help with Java Scripting API. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Need help with Java Scripting API." Watch "Need help with Java Scripting API." New topic
Author

Need help with Java Scripting API.

Dmitry Zhuravlev
Ranch Hand

Joined: Apr 14, 2010
Posts: 93
Greetings!

Ladys and Gentlemen, I need help with Java Scripting API.

I want to be able to manipulate fields and methods of instance of my class via embedded Javascript Rhino Engine. To do this I have written the following:



Unfortunately this doesn't work: it prints 'undefined'. But if I replace MyClass with ArrayList for example, the sane scheme works. I mean that I can get the size of my ArrayList by ArrayList.size(), for example. If object of my own class is put into the Engine the situatuin is different: I can't use its fields and methods.
If I evaluate the following: 'for (i in mc) { print(i + '\n ') }' it prints all the methods that respond to Object class, but not methods from MyClass.

So, as far as I understand, the problem is with my own class. Probably I must import it somehow to the Engine? Please, help.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

That code won't even compile; what code are you actually trying to run?

Right now you're printing the literal string "mc.myfield" (or would be if you were evaluating a string, which you're not, unless you didn't post all the relevant code).

Try correcting the code and following up.
Dmitry Zhuravlev
Ranch Hand

Joined: Apr 14, 2010
Posts: 93
OK, David, here is the full code with the same problem:




When I run runExample function it prints "undefined" if I try to print only field, and the following error if I want to run the Java Object mc method getInt():
undefinedjavax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: TypeError: Cannot find function getInt. (<Unknown source>#1) in <Unknown source> at line number 1
at com.sun.script.javascript.RhinoScriptEngine.eval(Unknown Source)
at com.sun.script.javascript.RhinoScriptEngine.eval(Unknown Source)
at javax.script.AbstractScriptEngine.eval(Unknown Source)
at solutiondatabase.ScriptingExample.runExample(ScriptingExample.java:26)
at solutiondatabase.Main.main(Main.java:49)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42596
    
  65
Try making the fields and the method public.


Ping & DNS - my free Android networking tools app
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

That's (mostly) correct--the scripting engine requires a publicly-accessible getter; it cannot directly access properties (at least it won't for me).
Dmitry Zhuravlev
Ranch Hand

Joined: Apr 14, 2010
Posts: 93
Thank you guys!

This works for functions. After I have marked my getInt function as public it started to work. Though I cannot get the 'myfield' anyway, if I make it public it throws an exception:

javax.script.ScriptException: sun.org.mozilla.javascript.internal.WrappedException: Wrapped java.lang.IllegalAccessException: Class sun.org.mozilla.javascript.internal.JavaMembers can not access a member of class solutiondatabase.ScriptingExample$MyClass with modifiers "public" (<Unknown source>#1) in <Unknown source> at line number 1
at com.sun.script.javascript.RhinoScriptEngine.eval(Unknown Source)
at com.sun.script.javascript.RhinoScriptEngine.eval(Unknown Source)
at javax.script.AbstractScriptEngine.eval(Unknown Source)
at solutiondatabase.ScriptingExample.runExample(ScriptingExample.java:33)
at solutiondatabase.Main.main(Main.java:49)


Otherwise the return is 'undefined'.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42596
    
  65
As David said, you apparently need public getters and setters for accessing fields, in line with how JavaBeans work.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with Java Scripting API.