我在尝试从 Java 应用程序客户端调用 EJB 方法时遇到问题。这是代码。
EJB 远程接口
package com.test;
import javax.ejb.Remote;
@Remote
public interface HelloBeanRemote {
public String sayHello();
}
EJB
package com.test;
import javax.ejb.Stateless;
@Stateless (name="HelloBeanExample" , mappedName="ejb/HelloBean")
public class HelloBean implements HelloBeanRemote {
@Override
public String sayHello(){
return "hola";
}
}
主类(另一个项目)
import com.test.HelloBeanRemote;
import javax.naming.Context;
import javax.naming.InitialContext;
public class Main {
public void runTest()throws Exception{
Context ctx = new InitialContext();
HelloBeanRemote bean = (HelloBeanRemote)ctx.lookup("java:global/Test/HelloBeanExample!com.test.HelloBeanRemote");
System.out.println(bean.sayHello());
}
public static void main(String[] args)throws Exception {
Main main = new Main();
main.runTest();
}
}
好吧,我的问题是什么?找不到此 EJB 的 JNDI 条目!
java.lang.NullPointerException
at com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:297)
at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:271)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:430)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at testdesktop.Main.runTest(Main.java:22)
at testdesktop.Main.main(Main.java:31) Exception in thread "main" javax.naming.NamingException: Lookup failed for 'java:global/Test/HelloBeanExample!com.test.HelloBeanRemote' in SerialContext [Root exception is javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext [Root exception is java.lang.NullPointerException]]
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:442)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at testdesktop.Main.runTest(Main.java:22)
at testdesktop.Main.main(Main.java:31) Caused by: javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext [Root exception is java.lang.NullPointerException]
at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:276)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:430)
... 3 more Caused by: java.lang.NullPointerException
at com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:297)
at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:271)
... 4 more Java Result: 1
我尝试了不同的 JNDI 条目,但没有任何效果(我从 NetBeans 控制台获得了这些条目):
信息:EJB HelloBeanExample 的可移植 JNDI 名称:[java:global/Test/HelloBeanExample, java:global/Test/HelloBeanExample!com.test.HelloBeanRemote]
信息:EJB HelloBeanExample 的 Glassfish 特定(非便携式)JNDI 名称:[ejb/HelloBean, ejb/HelloBean#com.test.HelloBeanRemote]
所以我尝试了以下条目,但我得到了同样的例外:
- java:global/Test/HelloBean 示例
- java:global/Test/HelloBeanExample!com.test.HelloBeanRemote
- ejb/HelloBean
- ejb/HelloBean#com.test.HelloBeanRemote
我正在使用 Netbeans 6.8 和 Glassfish v3!