这个开始把我逼疯了......
我想使用 Maven 创建一个 Glassfish 客户端应用程序。
为此,我添加了所需的 gf-client 依赖项:
<dependency>
<groupId>org.glassfish.appclient</groupId>
<artifactId>gf-client</artifactId>
<version>3.1</version>
<type>pom</type>
<scope>compile</scope>
</dependency>
然后,为了联系我的 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));
不幸的是,这样做时,我得到的只是
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
经过一些检查,我注意到我的 glassfish-naming-3.1.jar
出现在该客户端应用程序的 CLASSPATH 中。而且,根据 Eclipse 代码查找,这个 jar 应该包含 com.sun.enterprise.naming.impl.SerialInitContextFactory
。但是,如果在 Debug模式下,我执行 getClass().getClassLoader().getResource("com/sun/enterprise/naming/impl/SerialInitContextFactory")
它返回 null,这清楚地表明找不到类。
有关更多信息,JAR 是使用此插件配置从我的本地 Maven 存储库复制的:
<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>
此外,我必须承认,当使用相同的 API 运行简单测试以连接到 Glassfish 服务器时,绝对没有问题,这将我引向了 ClassLoader 问题。
当运行该客户端时,当前类加载器是(如 getClass().getClassLoader().getClass().getName()
所示)sun.misc.Launcher$AppClassLoader
。不幸的是,这与运行单元测试时完全相同。
那么,我该怎么做才能解决这个错误?
编辑 类存在于 glassfish-naming-3.1.jar
中,但似乎无法被标准类加载器找到。
编辑 一个有趣的发现:
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
同时
getClass().getClassLoader().getResource("com/sun/enterprise/naming/impl/SerialInitContextFactory") = null
最佳答案
出于我不完全理解的原因,在应用程序中运行时有一个已配置的上下文类加载器。这个类加载器似乎使用了某种 OSGi 命名限制。
因此,为了避免这个错误,我重置了上下文类加载器:
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
成功了!
关于java - 在 glassfish 命名中找不到 SerialInitContextFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7804357/