我有一个无状态豆
@Stateless
public class CalculatorImpl implements Calculator {
private static final long serialVersionUID = 1L;
@Override
public int add(int a, int b) {
return a+b;
}
@Override
public int sub(int a, int b) {
return a-b;
}
@Override
public int mul(int a, int b) {
return a*b;
}
@Override
public float div(int a, int b) {
return a/b;
}
}
我已将此部署在glassfish上,界面注释为@Remote
@Remote
public interface Calculator extends Serializable{
public int add(int a, int b)throws RemoteException;
public int sub(int a, int b)throws RemoteException;
public int mul(int a, int b)throws RemoteException;
public float div(int a, int b)throws RemoteException;
}
当我尝试通过客户端访问它时,即java桌面应用程序: 它抛出一个NullpointerException
客户代码:
MyClient client= new MyClient();
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
// glassfish default port value will be 3700,
// but im using tcpviewer to redirect my 50005 port to 3700
props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
InitialContext context = new InitialContext(props);
client.cart = (ShoppingCart) context
.lookup("java:global/EJBDemo2EAR/EJBDemo2/CalculatorImpl!com.java.stateless.Calculator");
System.out.println("In doget");
client.cart.buy("soap", 12);
client.cart.getCartContents();
错误是: 显示java.lang.NullPointerException at com.sun.enterprise.naming.impl.SerialContext.getORB(SerialContext.java:347) at com.sun.enterprise.naming.impl.SerialContext.getProviderCacheKey(SerialContext.java:354) at com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:384) at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:329) 在com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:477) at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:438) 在javax.naming.InitialContext.lookup(InitialContext.java:417) 在com.java.client.MyClient.main(MyClient.java:30) 线程“main”中的异常javax.naming.NamingException:SerialContext中的'java:global / EJBDemo2EAR / EJBDemo2 / CalculatorImpl!com.java.stateless.Calculator'查找失败[myEnv = {org.omg.CORBA.ORBInitialPort = 3700, java.naming.factory.initial = com.sun.enterprise.naming.SerialInitContextFactory,org.omg.CORBA.ORBInitialHost = localhost,java.naming.factory.url.pkgs = com.sun.enterprise.naming,java.naming。 factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [root异常是javax.naming.NamingException:无法为SerialContext获取SerialContextProvider [myEnv = {org.omg.CORBA.ORBInitialPort = 3700, java.naming.factory.initial = com.sun.enterprise.naming.SerialInitContextFactory,org.omg.CORBA.ORBInitialHost = localhost,java.naming.factory.url.pkgs = com.sun.enterprise.naming,java.naming。 factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is java.lang.NullPointerException]] 在com.sun.enterprise.naming.impl.SerialContext.lookup
答案 0 :(得分:1)
在context.lookup中只传递完全限定的接口名称:
client.cart=(ShoppingCart)context.lookup("com.java.stateless.Calculator");