Subj : Newbie help: using Java classes in Rhino scripts To : netscape.public.mozilla.jseng From : Brian J. Sayatovic Date : Thu Jan 20 2005 07:29 pm I'm trying to expose some Java objects to JavaScript. I seemt o be getting tripped up over a couple of things: 1. Does Rhino know how tro expose normal Java methods and bean-like properties, or do they all have to be prefixed with jsGEt_, jsSet_, jsFunction_, etc.? 2. What's the deal with package names? 3. Is there any way to get the source code without CVS? I don't have it installed, and would prefer to just have a ZIP so I can step through the source. The code I'm testing with is below. I'm just starting to expand into several permutations to understand what works and what doesn't. ------------------------------------------------------------ package test; import org.mozilla.javascript.ScriptableObject; public class FQFoo extends ScriptableObject { private String beanProperty = "FQFoo:bean"; private String jsProperty = "FQFoo:js"; public FQFoo() { super(); } public String getClassName() { String className = this.getClass().getName(); return className; } public String getBeanProperty() { return this.beanProperty; } public String jsGet_jsProperty() { return this.jsProperty; } } ------------------------------------------------------------ package test; import org.mozilla.javascript.ScriptableObject; public class PlainFoo extends ScriptableObject { private String beanProperty = "PlainFoo:bean"; private String jsProperty = "PlainFoo:js"; public PlainFoo() { super(); } public String getClassName() { String className = this.getClass().getName(); int lastDotIndex = className.lastIndexOf('.'); if(lastDotIndex < 0) { return className; } else { return className.substring(lastDotIndex + 1); } } public String getBeanProperty() { return this.beanProperty; } public String jsGet_jsProperty() { return this.jsProperty; } } ------------------------------------------------------------ // test1.js var mytest = JavaImporter(Packages.test); var string = ""; var plain = new PlainFoo(); var plainBean = plain.beanProperty; var plainJS = plain.jsProperty; var plainString = "" + plainBean + ";" + plainJS; with(mytest) { var fq = new FQFoo(); var fqBean = fq.beanProperty; var fqJS = fq.jsProperty; var fqString = "" + fqBean + ";" + fqJS; string = plainString + ", " + fqString; } string; ------------------------------------------------------------ import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import org.mozilla.javascript.Context; import org.mozilla.javascript.Scriptable; import org.mozilla.javascript.ScriptableObject; import test.FQFoo; import test.PlainFoo; /** * TODO Comment class in RhinoTest.java * * @author Trinition */ public class RhinoTest { private final Context context; private final Scriptable scope; public RhinoTest() throws InvocationTargetException, IllegalAccessException, InstantiationException { this.context = Context.enter(); this.scope = context.initStandardObjects(); ScriptableObject.defineClass(scope, PlainFoo.class); ScriptableObject.defineClass(scope, FQFoo.class); } public Object testString(String javaScript) { return this.context.evaluateString(this.scope, javaScript, "", 1, null); } public Object testFile(String filename) throws FileNotFoundException, IOException { FileReader reader = null; try { reader = new FileReader(filename); Object result = context.evaluateReader(scope, reader, filename, 1, null); return result; } finally { if(reader != null) { reader.close(); } } } public static void main(String[] args) { try { RhinoTest test = new RhinoTest(); String filename2 = "Test Data/test1.js"; Object result = test.testFile(filename2); System.out.println("Result: '" + result + "'"); } catch(Throwable t) { t.printStackTrace(); } } } .