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?
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.
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?
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
> 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.