Subj : Re: deploying new java objects to application that use Rhino as JS To : Eric Paschoalick Chaves From : Igor Bukanov Date : Tue Jan 28 2003 12:42 pm Eric Paschoalick Chaves wrote: > Hi Igor, > > >>I guess your application comes with some kind of a browser wich uses >>Rhino javascript, right? Could you give details about it? In any case I >>suggest not to extend from ScriptableObject, but simply use LiveConnect >>to access arbitrary classes. > > > You are totally correct. In fact this application is called Voice Browser; > it's a specialized browser that parse VXML (an XML standart for > voice-enabled applications). The choosen of extending scriptable object was > pointed to me as a possible solution for the needing of exposing new objects > to my browser. I'll take a look to LiveConnect and see if it fits. > > But since I start exploring this way, how should I do to add my > Counter.class to the "browser's rhino"? I'd like to finish this task at > least as a way to learn another trick, that could be usufull in future > situations, even if LiveConnect fulfill my needings. What you need to do is to call ScriptableObject.defineClass(Scriptable scope, Class javaClass) with passing a proper scope where defineClass will put a JavaScript constructor for your object. In Rhino shell scripts can access defineClass function that calls ScriptableObject.defineClass with a proper scope, but if the Rhino embedding does not provide a convenient way to do it, you can try LiveConnect machinery to invoke from your script ScriptableObject.defineClass directly (you still need to make sure that a Counter class is available on your CLASSPATH): Packages.org.mozilla.javascript.ScriptableObject.defineClass(this, Packages.Counter) where "this" gives you a reference to a scope object to define script top level variables. If this scope is recreated each time scripts run, you can try to replace "this" in that fragment by Math.__parent__ to define your object in the same scope where all standard objects and functions lives. Regards, Igor .