• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Tim Cooke
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
Saloon Keepers:
  • Piet Souris
Bartenders:

Need help with Java Scripting API.

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try making the fields and the method coderanch.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As David said, you apparently need public getters and setters for accessing fields, in line with how JavaBeans work.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic