我们希望将groovy / grails用作Glassfish-Remote-Client,但我们遇到了奇怪的ClassLoading问题。
首先通过groovy / grails调用时触发错误的Testcode。 通过java调用时没关系。 当通过groovy运行时,它会触发一个 java.lang.ClassCastException:com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject无法强制转换为javax.rmi.CORBA.PortableRemoteObjectDelegate in(PortableRemoteObject.java:77) 在该错误之前,可以手动进行转换。
========== source ==========
package test;
import java.lang.reflect.Field;
public class J2EETest {
public static void main(String[] args) throws Exception {
new J2EETest().classLoaderTest();
}
public static void classLoaderTest() throws Exception {
ClassLoader classLoader = J2EETest.class.getClassLoader();
ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader();
System.out.println("classLoader: " + classLoader);
System.out.println("threadClassLoader: " + threadClassLoader);
Class klass = Class.forName( "com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject", false, threadClassLoader );
Object obj = klass.newInstance();
System.out.println("Object -> " + obj.toString());
// This works
javax.rmi.CORBA.PortableRemoteObjectDelegate del = (javax.rmi.CORBA.PortableRemoteObjectDelegate) obj;
try {
System.out.println("Init of javax.rmi.PortableRemoteObject");
// This doesn't work !
System.out.println( Class.forName("javax.rmi.PortableRemoteObject", true, threadClassLoader));
Class prOklass = javax.rmi.PortableRemoteObject.class;
Field staticfield = prOklass.getDeclaredField("proDelegate");
staticfield.setAccessible(true);
System.out.println("Reflection: PortableRemoteObject.proDelegate ->" + staticfield.get(null));
System.out.println("OK");
}
catch(Throwable e) {
System.err.println("BOOM");
e.printStackTrace();
}
}
}
======= JAVA测试运行=======
$ export CLASSPATH=build:lib/glassfish/gf-client-module.jar # gf-client-module.jar -> GLASSFISH-Client Libs
$ java -Djavax.rmi.CORBA.PortableRemoteObjectClass=com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject test.J2EETest
classLoader: sun.misc.Launcher$AppClassLoader@e776f7
threadClassLoader: sun.misc.Launcher$AppClassLoader@e776f7
Object -> com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject@5511e28
Init of javax.rmi.PortableRemoteObject class javax.rmi.PortableRemoteObject
Reflection: PortableRemoteObject.proDelegate -> com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject@20e5f01b
OK
=======现在是Groovy-Version =======
$ groovy -Djavax.rmi.CORBA.PortableRemoteObjectClass=com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject -e "test.J2EETest.classLoaderTest()"
classLoader: org.codehaus.groovy.tools.RootLoader@5dcba031
threadClassLoader: org.codehaus.groovy.tools.RootLoader@5dcba031
Object -> com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject@1bf3f158
Init of javax.rmi.PortableRemoteObject
BOOM
java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at test.J2EETest.classLoaderTest(J2EETest.java:83)
at test.J2EETest$classLoaderTest.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
at script_from_command_line.run(script_from_command_line:1)
at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:266)
at groovy.lang.GroovyShell.run(GroovyShell.java:517)
at groovy.lang.GroovyShell.run(GroovyShell.java:172)
at groovy.ui.GroovyMain.processOnce(GroovyMain.java:553)
at groovy.ui.GroovyMain.run(GroovyMain.java:337)
at groovy.ui.GroovyMain.process(GroovyMain.java:323)
at groovy.ui.GroovyMain.processArgs(GroovyMain.java:120)
at groovy.ui.GroovyMain.main(GroovyMain.java:100)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:108)
at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:130)
Caused by: java.lang.ClassCastException: com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject cannot be cast to javax.rmi.CORBA.PortableRemoteObjectDelegate at javax.rmi.PortableRemoteObject.<clinit>(PortableRemoteObject.java:77)
... 22 more
=======现在Groovy-Version WITHOUT -Djavax.rmi.CORBA.PortableRemoteObjectClass =======
这是有效的,但不是glassfish客户端的选项,因为InitialContext(或其他东西)将设置javax.rmi.CORBA.PortableRemoteObjectClass = com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject
$ groovy -e "test.J2EETest.classLoaderTest()"
classLoader: org.codehaus.groovy.tools.RootLoader@3b4d82e1
threadClassLoader: org.codehaus.groovy.tools.RootLoader@3b4d82e1
Object -> com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject@527e2f47
Init of javax.rmi.PortableRemoteObject
class javax.rmi.PortableRemoteObject
Reflection: PortableRemoteObject.proDelegate -> com.sun.corba.se.impl.javax.rmi.PortableRemoteObject@5b3bd1c0 # com.sun.corba.ee != com.sun.corba.se
好的
是否有人遇到类似问题或知道如何使此代码正常工作?
亲切的问候
谢
答案 0 :(得分:0)
我试着运行你的例子,我得到了:
classLoader: groovy.lang.GroovyClassLoader$InnerLoader@538d7ace
threadClassLoader: groovy.lang.GroovyClassLoader@1e22ab57
Caught: java.lang.ClassNotFoundException: com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject
java.lang.ClassNotFoundException: com.sun.corba.ee.impl.javax.rmi.PortableRemoteObject
at java_lang_Class$forName.call(Unknown Source)
at test.J2EETest.classLoaderTest(J2EETest.groovy:16)
at test.J2EETest$classLoaderTest.call(Unknown Source)
at test.J2EETest.main(J2EETest.groovy:7)
我能建议的最好的办法是隔离不同的东西。最可能的罪魁祸首是你在两种情况下使用的JVM。将用于执行groovy的JVM与用于直接运行jar的JVM进行比较。我还建议将groovy-all.jar放入Classpath并尝试从Java运行Groovy解释器。