InitialContext中的NullPointerException
private void connect() {
try {
InitialContext ctx = new InitialContext();
IServerBean serverBean = (IServerBean)ctx.
lookup("java:global/applicationserver/ServerBean!
com.test.applicationserver.IServerBean");
} catch (NamingException e) {
logger.error(e.getMessage(), e);
}
}
尝试通过控制台java -cp cleint-0.0.1-SNAPSHOT-jar-with-dependencies.jar com.test.client.EJBClient
执行客户端应用程序时出现此异常:
java.lang.NullPointerException
at com.sun.enterprise.naming.impl.SerialContext.<init>(SerialContext.java:276)
at com.sun.enterprise.naming.impl.SerialContext.<init>(SerialContext.java:335)
at com.sun.enterprise.naming.impl.SerialInitContextFactory.createInitialContext
(SerialInitContextFactory.java:358)
at com.sun.enterprise.naming.impl.SerialInitContextFactory.getInitialContext
(SerialInitContextFactory.java:353)
at com.sun.enterprise.naming.SerialInitContextFactory.getInitialContext
(SerialInitContextFactory.java:69)
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.init(Unknown Source)
at javax.naming.InitialContext.<init>(Unknown Source)
at com.test.client.EJBClient.connect(EJBClient.java:33)
at com.test.client.EJBClient.main(EJBClient.java:61)
此应用程序连接到远程EJB模块。 但是当我尝试在Eclipse中执行它时,一切都很顺利。 我的配置:
Maven依赖项:
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.4.2</version>
<scope>system</scope>
<systemPath>C:\Java\jdk1.7.0_11\lib\tools.jar</systemPath>
</dependency>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>javaee</artifactId>
<version>3.1.2.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.appclient</groupId>
<artifactId>gf-client</artifactId>
<version>3.1.1</version>
</dependency>
</dependencies>
答案 0 :(得分:1)
我遇到了同样的问题。 解决 : 您必须不仅在项目的类路径中添加gf-client.jar和appserv-rt.jar,还要在运行时添加(使用Eclipse,请参阅运行方式&gt;运行配置&gt;类路径&gt;添加罐子。)
答案 1 :(得分:0)
我用这个解决方案解决了这个问题:
使用应用程序将此库添加到文件夹:
运行应用程序时,将此库添加到classpath:
java -cp glassfish-embedded.jar;hk2-core.jar;internal-api.jar;common-util.jar;glassfish-corba-internal-api.jar;glassfish-naming.jar;cleintconsole-0.0.1-SNAPSHOT-jar-with-dependencies.jar com.test.client.EJBClient