Closed Bug 200551 Opened 22 years ago Closed 22 years ago

JavaAdapter not loading a class if js.jar installed in jre/lib/ext directory

Categories

(Rhino Graveyard :: Core, defect)

x86
Windows XP
defect
Not set
normal

Tracking

(Not tracked)

VERIFIED FIXED
1.5R4.1

People

(Reporter: rivasdiaz, Assigned: norrisboyd)

References

Details

Attachments

(3 files, 3 obsolete files)

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.2) Gecko/20021126 Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.2) Gecko/20021126 In my app I execute some javascript files, they use the constructor JavaAdapter to load and extend java clases, some of them abstract clases extended in javascript code. If I run my app this way: java -cp js.jar;myapp.jar myapp.Main everythigs work ok, but when I put js.jar on the jre/lib/ext directory, and execute java -jar myapp.jar when my code is trying to execute the javascript file I get an error on the line: new JavaAdapter(...); The error is: TypeError: expected java class object (jar:file:/C:/Work/Eclipse/docgen.jar!/com/prospectiva/templates/poder_general.js; line 547) at org.mozilla.javascript.NativeGlobal.constructError(NativeGlobal.java:597) at org.mozilla.javascript.NativeGlobal.constructError(NativeGlobal.java:557) at org.mozilla.javascript.JavaAdapter.jsConstructor(JavaAdapter.java:100) at inv1.invoke() at org.mozilla.javascript.FunctionObject.doInvoke(FunctionObject.java:498) at org.mozilla.javascript.FunctionObject.callVarargs(FunctionObject.java:518) at org.mozilla.javascript.FunctionObject.construct(FunctionObject.java:450) at org.mozilla.javascript.ScriptRuntime.newObject(ScriptRuntime.java:1265) at org.mozilla.javascript.gen.c12.call(jar:file:/C:/Work/Eclipse/docgen.jar!/com/prospectiva/templates/poder_general.js:547) at org.mozilla.javascript.optimizer.OptRuntime.callSimple(OptRuntime.java:275) at org.mozilla.javascript.gen.c13.call(jar:file:/C:/Work/Eclipse/docgen.jar!/com/prospectiva/templates/poder_general.js:553) at org.mozilla.javascript.gen.c13.exec(jar:file:/C:/Work/Eclipse/docgen.jar!/com/prospectiva/templates/poder_general.js) at org.mozilla.javascript.Context.evaluateReader(Context.java:820) Reproducible: Always Steps to Reproduce: 1. just put js.jar on the jre/lib/ext directory and execute a javascript program that uses new JavaAdapter(...) Actual Results: Exception thrown Expected Results: instantiate a new class
cc'ing Igor -
Rivas, which version of Rhino do you use? If it is 1.5R4, try 1.5R3 or the latest build from CVS, it can be that you are affected by the regression bug 196017 in 1.5R4.
I use rhino 1.5r4 and j2sdk 1.4.1_02 I checked the other bug but i can't see if it's related. My app works perfectly if it is on the classpath, but fails if it is in jre/ext/lib. I'll download tonight the new j2sdk 1.4.2 beta to test it. thanks.
As regarding bug 196017, although its reasons are different, the fix there may help in your situation as well. So could you try Rhino build from CVS tip from ftp://ftp.mozilla.org/pub/js/rhinoLatest.zip ?
A detailed test case: 1. Copy js.jar from a Rhino distribution to lib/ext directory from JDK/JRE installation. 2. Save the following Java source as MyTest.java in some directory and compile it: public abstract class MyTest { public abstract int test(); public static int runTest (MyTest x) { return x.test(); } } 3. Save the following JS as test.js in the same directory: var o = { test: function() { return 100; } }; var test_instance = new JavaAdapter(Packages.MyTest, o); var i = Packages.MyTest.runTest(test_instance); print(i); 4. Run from this directory: java -classpath . org.mozilla.javascript.tools.shell.Main -f test.js I got with 1.5R4: js: "/home/igor/w/js/test.js", line 3: uncaught JavaScript exception: TypeError: expected java class object (/home/igor/w/js/test.js; line 3) while js.jar from 1.5R3 gives the proper output: 100 If the same is run
The fix in the bug 196017 does not fix the problem since with the current Rhino tip I got the same problem: js: "/home/igor/w/js/test.js", line 3: uncaught JavaScript exception: TypeError: expected java class object (/home/igor/w/js/test.js; line 3). The reason for this is that the fix only restored search for thread context loader when defining classes and not when doing class search in NativeJavaPackage.
Status: NEW → ASSIGNED
The patch adds Context.getApplicationClassLoader() that is now used in all cases as a parent loader for generated classes and as the default class loader for NativeJavaPackage. The default implementation tries to use Thread.getContextClassLoader, but only when it is available and if Rhino classes is available through it. Otherwise the loader for Context instance is used. In this way if Rhino is loaded through a custom loader, it will be used, and if Rhino classes are placed in lib/ext, Thread.getContextClassLoader still give the application loader. And if this default policy would not work in a particular application, Context.getApplicationClassLoader() can be overridden to in that application. I believe in this way all functionality of Rhino 1.5R3 regarding class loading is restored while making sure that fixes that triggers class loading changes in 1.5R4 are not affected.
I changed the patch to provide explicit getter/setter for application class loader so changing the application loader can be done without subclassing Context and a custom loader will be checked that current Rhino classes is accessible through it. Moreover, the method can be called from scripts directly to help to identify class loader problems quickly.
Attachment #120308 - Attachment is obsolete: true
In the previous version of the patch NativeJavaPackage called Context.getApplicationClassLoader() only in its constructor effectively ignoring changes subsequent changes in Context.getApplicationClassLoader(). The new version calls the method each time a new class is accessed.
Attachment #120370 - Attachment is obsolete: true
I committed the last version of the patch
Status: ASSIGNED → RESOLVED
Closed: 22 years ago
Resolution: --- → FIXED
Blocks: 196149
I committed the fix to Rhino150R4_BRANCH as well
Verified FIXED. Following the precise steps Igor gave in Comment #5, I copied js.jar from different Rhino 1.5 releases to C:\Program Files\Java\j2re1.4.1\lib\ext\ The output I got before this fix were the same as Igor's results: 1.5R3 ---> 100 1.5R4 ---> JavaScript exception: TypeError: expected java class object 1.5R5pre ---> JavaScript exception: TypeError: expected java class object Now, with 1.5R5pre after this fix, I get 100 as output -
Status: RESOLVED → VERIFIED
Targeting as resolved against 1.5R4.1
Target Milestone: --- → 1.5R4.1
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: