problem importing classes in JavaScript

Hi,

I am using the built in rhino engine in Java 6 to execute a javascript file within my program. I am using some java classes inside the javascript, and the importClass declarations seem to be causing some problems. I have my import declarations laid out like:

importClass(x.y.foo.ClassName);

importClass(x.y.foo.ClassName2);

and the majority of the time these classes are loaded without problem. Sometimes however the class fails to load, and an exception like this is thrown:

Problem loading script: myRhinoScript.js

javax.script.ScriptException: sun.org.mozilla.javascript.internal.EvaluatorException: Function importClass must be called with aclass; had"[JavaPackage x.y.foo.ClassName]" instead. (<Unknown source>#1) in <Unknown source> at line number 1

Apparently this is because rhino can't find that class and is instead interpreting the string as a package. Therefore an attempt to call "importClass" with a package, throws the above error.

How can I make sure that this rhino script can always find my classes?

[1150 byte] By [ceilingfisha] at [2007-11-27 8:50:30]
# 1
Can you make use of those class file in a "normal" Java application? If not, my guess it's a CLASSPATH issue.
prometheuzza at 2007-7-12 21:01:49 > top of Java-index,Java Essentials,Java Programming...
# 2

I get the same kind of issue with inbuilt java classes, sometimes they load sometimes they don't. My classes are available on the class path (as far as I understand). My worry was that they were not available to the class loader. I tried changing the class loader for the script engine using:

final ScriptEngineManager factory = new ScriptEngineManager(Object.class.getClassLoader().getSystemClassLoader());

ScriptEngineManager is the abstract factory that handles all the script engine objects, but this doesn't seem to have had much of an effect.

ceilingfisha at 2007-7-12 21:01:49 > top of Java-index,Java Essentials,Java Programming...
# 3
> I get the same kind of issue with inbuilt java> classes, sometimes they load sometimes they don't. Then it's sometimes a CLASSPATH issue, and sometimes it's not.
prometheuzza at 2007-7-12 21:01:49 > top of Java-index,Java Essentials,Java Programming...
# 4
Is there an easy way I can determine resources are available to the script engine?How can I explicitly make resources available to it?
ceilingfisha at 2007-7-12 21:01:49 > top of Java-index,Java Essentials,Java Programming...
# 5
> Is there an easy way I can determine resources are> available to the script engine?> > How can I explicitly make resources available to it?Err, by setting your CLASSPATH properly?
prometheuzza at 2007-7-12 21:01:49 > top of Java-index,Java Essentials,Java Programming...
# 6

Umm, well I currently am trying to load classes from the war file in which the application is executing (so those files are on the classpath, aren't they?), and additionally packages from:

java.io

java.util

java.lang

java.net

All these are available on the classpath too automatically as well, aren't they? I mean they're shipped in the jars that are attached to the JVM.

What else could I do to include these classes on the classpath?

ceilingfisha at 2007-7-12 21:01:49 > top of Java-index,Java Essentials,Java Programming...
# 7
> ...> What else could I do to include these classes on the> classpath?Have a look at this document: http://java.sun.com/j2se/1.3/docs/tooldocs/win32/classpath.html
prometheuzza at 2007-7-12 21:01:49 > top of Java-index,Java Essentials,Java Programming...
# 8

Ah, yes, but this code isn't executed from the command line, it's a WAR file, ie it's deployed using an application server.

I tried setting the CLASSPATH global variable to point to a jar file containing a second copy of the code that is packaged inside the WAR file, on the suspicion that rhino engine might only resolve the names by looking up the classes on the class path, but this only produced two versions of my webapp.

Is there anywhere else more specialist to JSR 223 scripting that I could pose this question?

Tom

ceilingfisha at 2007-7-12 21:01:49 > top of Java-index,Java Essentials,Java Programming...
# 9
> Ah, yes, but this code isn't executed from the> command line, it's a WAR file, ie it's deployed using> an application server.> >...Then try reading the manual of you application server how to add things to your CLASSPATH.
prometheuzza at 2007-7-12 21:01:49 > top of Java-index,Java Essentials,Java Programming...