简体   繁体   English

无法从 Swing 客户端创建初始上下文

[英]Cannot create an initial context from a Swing client

Please see the code below:请看下面的代码:

try {
            //InitialContext ic = new InitialContext();
            Hashtable env = new Hashtable();
            env.put(Context.INITIAL_CONTEXT_FACTORY,  
             "com.sun.enterprise.naming.SerialInitContextFactory");
            Context ctx = new InitialContext(env);      

            MySessionRemote hb = (MySessionRemote) ctx.lookup("MySessionRemote");
            System.out.println(hb.getResult());
        } catch (NamingException ex) {
            ex.printStackTrace();
        }

The error I get from a Swing app is :(javax.naming.NoInitialContextException) javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.enterprise.naming.SerialInitContextFactory [Root exception is java.lang.ClassNotFoundException: com.sun.enterprise.naming.SerialInitContextFactory] The error I get from a Swing app is :(javax.naming.NoInitialContextException) javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.enterprise.naming.SerialInitContextFactory [Root exception is java.lang.ClassNotFoundException: com.sun.enterprise.naming.SerialInitContextFactory]

I have not worked with Java for a while so this could be quite straightforward.我有一段时间没有使用 Java,所以这可能很简单。

Update更新

I am using version 4.1 of Glassfish.我正在使用 Glassfish 4.1 版。 I have added references to the following jars inside the NetBeans project:我在 NetBeans 项目中添加了对以下 jars 的引用:

appserv-rt.jar
gf-client.jar
javaee.jar

I can now get an InitialContext.我现在可以获得一个 InitialContext。 However, I cannot lookup the bean ie this line fails:但是,我无法查找 bean,即这条线失败:

MySessionRemote hb = (MySessionRemote) ctx.lookup("ejb.MySessionRemote");

The exception is a NamingException ie Lookup Failed .例外是NamingExceptionLookup Failed I believe the name of the bean may be wrong.我相信bean的名称可能是错误的。 Here is the bean:这是豆:

package ejb; package ejb;

import javax.annotation.Resource;
import javax.ejb.Remote;
@Resource(name = "MySessionRemote")
@Remote
public interface MySessionRemote {
    public String getResult();
}

What should the name of the bean be?豆子的名字应该是什么?

Which application server are you using?您使用的是哪个应用程序服务器? Eg when using Glassfish that class can be found in the glassfish-naming.jar file.例如,当使用 Glassfish 时,class 可以在glassfish-naming.jar文件中找到。 That JAR needs to be on your classpath. JAR 需要在您的类路径中。 I assume other application servers have a similar JAR file.我假设其他应用程序服务器具有类似的 JAR 文件。

In fact, when you have that JAR on your classpath you could simply use the no-args constructor of InitialContext since the JAR already contains a jndi.properties file with the following content:事实上,当你的类路径中有 JAR 时,你可以简单地使用InitialContext的无参数构造函数,因为 JAR 已经包含一个jndi.properties文件,其内容如下:

java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory
java.naming.factory.url.pkgs=com.sun.enterprise.naming
# Required to add a javax.naming.spi.StateFactory for CosNaming that
# supports dynamic RMI-IIOP.
java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM