gpt4 book ai didi

java - EJB 远程客户端抛出 nullpointerException : Unable to acquire SerialContextProvider

转载 作者:行者123 更新时间:2023-11-30 02:27:59 25 4
gpt4 key购买 nike

我有一个无状态 bean

 @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 在 com.sun.enterprise.naming.impl.SerialContext.getORB(SerialContext.java:347) 在 com.sun.enterprise.naming.impl.SerialContext.getProviderCacheKey(SerialContext.java:354) 在 com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:384) 在 com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:329) 在 com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:477) 在 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[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} [根异常是 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} [根异常是 java.lang.NullPointerException]] 在 com.sun.enterprise.naming.impl.SerialContext.lookup

最佳答案

在 context.lookup 中只需传递完全限定的接口(interface)名称:

client.cart=(ShoppingCart)context.lookup("com.java.stateless.Calculator");

关于java - EJB 远程客户端抛出 nullpointerException : Unable to acquire SerialContextProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45121893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com