java 在 glassfish 命名中找不到 SerialInitContextFactory
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7804357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SerialInitContextFactory not found in glassfish naming
提问by Riduidel
This one starts to drive me completely nuts ...
这个开始让我完全疯了......
I want to create a Glassfish client application using Maven.
我想使用 Maven 创建一个 Glassfish 客户端应用程序。
For that, I've added the requried gf-client dependency :
为此,我添加了必需的 gf-client 依赖项:
<dependency>
<groupId>org.glassfish.appclient</groupId>
<artifactId>gf-client</artifactId>
<version>3.1</version>
<type>pom</type>
<scope>compile</scope>
</dependency>
Then, wanting to contact my Glassfish server, running on the same application, I do the usual lookup :
然后,想要联系运行在同一应用程序上的 Glassfish 服务器,我进行了通常的查找:
Properties p = new Properties();
// optional. Defaults to localhost. Only needed if web server is running
// on a different host than the appserver
p.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
// optional. Defaults to 3700. Only needed if target orb port is not
// 3700.
p.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
Context context = new InitialContext(p);
// Stores the list of reachable EJBs
return context.lookup(interfacesToNames.getProperty(className));
Unfortunatly, when doing so, all I get is
不幸的是,这样做时,我得到的只是
Caused by: javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.enterprise.naming.impl.SerialInitContextFactory [Root exception is java.lang.ClassNotFoundException: com/sun/enterprise/naming/impl/SerialInitContextFactory]
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:674)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
at javax.naming.InitialContext.init(InitialContext.java:242)
at javax.naming.InitialContext.<init>(InitialContext.java:216)
... 6 more
Caused by: java.lang.ClassNotFoundException: com/sun/enterprise/naming/impl/SerialInitContextFactory
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:63)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:671)
... 9 more
After some checks, I've noticed that my glassfish-naming-3.1.jar
is present in CLASSPATH for that client application. And, according to Eclipse code lookup, it is this jar that should contain com.sun.enterprise.naming.impl.SerialInitContextFactory
. However, if in debug mode, I do getClass().getClassLoader().getResource("com/sun/enterprise/naming/impl/SerialInitContextFactory")
it returns me null, which clearly indicates that the class can't be found.
经过一些检查,我注意到 myglassfish-naming-3.1.jar
存在于该客户端应用程序的 CLASSPATH 中。而且,根据 Eclipse 代码查找,这个 jar 应该包含com.sun.enterprise.naming.impl.SerialInitContextFactory
. 但是,如果在调试模式下,我这样做getClass().getClassLoader().getResource("com/sun/enterprise/naming/impl/SerialInitContextFactory")
会返回 null,这清楚地表明找不到该类。
For more infos, the JAR is copied from my local maven repository using this plugin configuration :
有关更多信息,请使用以下插件配置从我的本地 Maven 存储库复制 JAR:
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>package output directory</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<prependGroupId>true</prependGroupId>
<includeScope>compile</includeScope>
<outputDirectory>${dependencies.outputDir}/jars</outputDirectory>
<prefix>${dependencies.outputDir}</prefix>
</configuration>
</execution>
</executions>
</plugin>
Furthermore, I have to confess that, when running a simple test using the same API to connect to Glassfish server, there is absolutely no problem, which directs me to a ClassLoader issue.
此外,我必须承认,当使用相同的 API 运行一个简单的测试以连接到 Glassfish 服务器时,绝对没有问题,这将我引向 ClassLoader 问题。
When running that client, the current class loader is (as denoted by getClass().getClassLoader().getClass().getName()
) sun.misc.Launcher$AppClassLoader
. Which unfortunatly is exactly the same than when running unit test.
运行该客户端时,当前类加载器为(由 表示getClass().getClassLoader().getClass().getName()
)sun.misc.Launcher$AppClassLoader
。不幸的是,这与运行单元测试时完全相同。
So, what can I do to solve that bug ?
那么,我该怎么做才能解决该错误?
EDITClass exists in glassfish-naming-3.1.jar
, but does not seems to be findable by standard classloader.
EDIT类存在于 中glassfish-naming-3.1.jar
,但标准类加载器似乎无法找到。
EDITAn interesting discovery :
编辑一个有趣的发现:
getClass().getClassLoader().getResource("com/sun/enterprise/naming/impl") = jar:file:/C:/Users/pouet/pouet/target/jars/glassfish-naming-3.1.jar!/com/sun/enterprise/naming/impl
while
尽管
getClass().getClassLoader().getResource("com/sun/enterprise/naming/impl/SerialInitContextFactory") = null
采纳答案by Riduidel
For a reason I not totally understand, there is a configured Context classloader when run in application. This classloader seems to use some kind of OSGi naming restriction.
出于我不完全理解的原因,在应用程序中运行时有一个已配置的 Context 类加载器。这个类加载器似乎使用了某种 OSGi 命名限制。
As a consequence, to avoid the bug, I reset the context class loader :
因此,为了避免错误,我重置了上下文类加载器:
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
And it worked !
它奏效了!
回答by Alexis MP
Have you tried using the ACC? Your solution seems contrived.
你试过使用ACC吗?您的解决方案似乎是人为的。