This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help with Java Scripting API.

 
Dmitry Zhuravlev
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try making the fields and the method public.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As David said, you apparently need public getters and setters for accessing fields, in line with how JavaBeans work.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic