我一直有問題試圖從Java應用程序客戶端調用EJB的方法。 這是代碼。Glassfish v3/JNDI入門無法找到問題!
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:全球/測試/ HelloBeanExample, Java的:全球/測試/ HelloBeanExample com.test.HelloBeanRemote]
信息
:對於EJB HelloBeanExample Glassfish的特異性(非便攜式)的JNDI名稱:EJB /爲helloBean,EJB /爲helloBean#com.test.HelloBeanRemote]所以我嘗試了以下entri ES,但我得到了同樣的異常:
- 的java:全球/測試/ HelloBeanExample
- 的java:全球/測試/ HelloBeanExample com.test.HelloBeanRemote
- EJB /爲helloBean
- EJB /爲helloBean# com.test.HelloBeanRemote
我使用Netbeans 6.8和Glassfish v3!