Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

importClass HelloWorld doesn't work

175 views
Skip to first unread message

Clifton B. Sothoron Jr.

unread,
Mar 19, 2010, 9:22:12 PM3/19/10
to dev-tech-js-...@lists.mozilla.org
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?

Best,

Clifton Sothoron

Martin Blom

unread,
Mar 21, 2010, 10:56:43 AM3/21/10
to Clifton B. Sothoron Jr., dev-tech-js-...@lists.mozilla.org

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:

[martin@elsa ~]$ cat<<EOF> HelloWorld.java

> public class HelloWorld {

> public HelloWorld() {}

> }

> EOF

[martin@elsa ~]$ cat<<EOF> HelloWorld.js

> importClass(Packages.HelloWorld);

> var hw = new HelloWorld();

> java.lang.System.out.println(java.lang.Class.forName('HelloWorld'));

> EOF

[martin@elsa ~]$ javac HelloWorld.java

[martin@elsa ~]$ java -Xbootclasspath/p:. -jar ~/source/external/JavaScript/rhino/build/rhino1_7R3pre/js.jar HelloWorld.js

class HelloWorld

[martin@elsa ~]$


--
---- Martin Blom --------------------------- mar...@blom.org ----
Eccl 1:18 http://martin.blom.org/

0 new messages