On 20/03/2010 02:22, Clifton B. Sothoron Jr. wrote:
I've been experimenting with the Rhino 1.7 JavaScript to Java compiler.
I've had some success but a very simple thing is giving me grief. I'm
trying to import a java class into a .js file. This works just fine with
built-in java classes but when I import anything else it fails. The
CLASSPATH includes the current directory. The HelloWorld is in the
current directory. The HelloWorld runs properly. However, the following
script compiles properly with org.mozilla.javascript.tools.jsc.Main, but
fails at runtime.
importClass(HelloWorld);
var hw = new HelloWorld();
java.lang.System.out.println(java.lang.Class.forName('HelloWorld'));
Exception in thread "main" org.mozilla.javascript.EcmaError:
ReferenceError: "HelloWorld" is not defined. at
org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3
654)
Why can't I import in a class that is not part of Java?
1. The classpath is not used when you execute a jar file, in case you
run Rhino like that. You need to put the class in your
bootclasspath instead.
2. You will find you HelloWorld class inside the Packages top-level
object.
For example:
[mar...@elsa ~]$ cat<<EOF> HelloWorld.java
public class HelloWorld {
public HelloWorld() {}
}
EOF
[mar...@elsa ~]$ cat<<EOF> HelloWorld.js
importClass(Packages.HelloWorld);
var hw = new HelloWorld();
java.lang.System.out.println(java.lang.Class.forName('HelloWorld'));
EOF
[mar...@elsa ~]$ javac HelloWorld.java
[mar...@elsa ~]$ java -Xbootclasspath/p:. -jar
~/source/external/JavaScript/rhino/build/rhino1_7R3pre/js.jar HelloWorld.js
class HelloWorld
[mar...@elsa ~]$
--
---- Martin Blom --------------------------- [email protected] ----
Eccl 1:18 http://martin.blom.org/
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino