0

我正在使用 netbeans IDE 7.1.2,我创建了一个 EJB 3.1 Stateless Bean 并实现了远程和本地接口,并将其成功部署到 glassfish 服务器 3.2.1 ......这是代码

__ _ __ _ __ _ __ _ __ _ __ _ ___ CaseInfo.java _ __ _ __ _ __ _ __ _ __ _ __

enter code here

package maxx;

import javax.ejb.Stateless;

@Stateless(name="CaseInfo",mappedName="CaseInfoBean")
public class CaseInfo implements CaseInfoRemote, CaseInfoLocal {

@Override
public String MyCaseMessageLocal() {
    return "Hello this is my Local message.";
}

@Override
public String MyCaseMessageRemote() {
    return "Hello this is my Remote message.Please Note that this message is comming from the remote";
}

}

__ _ __ _ __ _ __ _ __ _ __ _ CaseInfoLocal.java _ __ _ __ _ __ _ __ _ __ _ ___

package maxx;

import javax.ejb.Local;

@Local
public interface CaseInfoLocal {

String MyCaseMessageLocal();

}

__ _ __ _ __ _ __ _ __ _ __ CaseInfoRemote.java _ __ _ __ _ __ _ __ _ __ _ __

package maxx;

import javax.ejb.Remote;

@Remote
public interface CaseInfoRemote {

String MyCaseMessageRemote();

}


现在我已经成功地从本地访问了它,在它创建新项目>Java EE>Application Client 项目后,我已经通过将 bean 模块的 jar 文件作为客户端存根添加到其库中成功访问了该 bean这是一些代码

__ _ __ _ __ _ __ _ __ _ __ _ ___ Main.java _ __ _ __ _ __ _ __ _ _ _ __ _ __

package applicationclient;

import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import maxx.CaseInfoRemote;

public class Main {


public static void main(String arg[]) {

    try {
Properties prop = new Properties();
prop.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                          "com.sun.enterprise.naming.SerialInitContextFactory");
prop.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
prop.setProperty("org.omg.CORBA.ORBInitialPort", "3700"); 

        InitialContext ic = new InitialContext(prop);
        Object ref = ic.lookup("CaseInfoBean");
        CaseInfoRemote home = (CaseInfoRemote)PortableRemoteObject.narrow(ref,
                CaseInfoRemote.class);
        System.out.println(home.MyCaseMessageRemote());

    } catch(Exception e) {
        e.printStackTrace();
    }
}
}

但是当我使用此客户端访问代码时,请转到 New Project>java>java Application 并在此处编写相同的代码,因此问题表明

__ _ __ _ __ _ __ _ __ _ __ _ ____错误_ __ _ __ _ __ _ __ _ __ _ __ _ __ _ _

run:
javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.enterprise.naming.SerialInitContextFactory [Root exception is  java.lang.ClassNotFoundException: com.sun.enterprise.naming.SerialInitContextFactory]
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:674)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
at javax.naming.InitialContext.init(InitialContext.java:242)
at javax.naming.InitialContext.<init>(InitialContext.java:216)
at testcasebean.TestCaseBean.main(TestCaseBean.java:30)
Caused by: java.lang.ClassNotFoundException: com.sun.enterprise.naming.SerialInitContextFactory
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:63)
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:671)
    ... 4 more
BUILD SUCCESSFUL (total time: 0 seconds)

如果我要删除代码的属性行,那么会给出这种类型的错误

运行:javax.naming.NoInitialContextException:需要在环境或系统属性中指定类名,或作为小程序参数,或在应用程序资源文件中:

java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344)
    at javax.naming.InitialContext.lookup(InitialContext.java:411)
    at testcasebean.TestCaseBean.main(TestCaseBean.java:27)
BUILD SUCCESSFUL (total time: 0 seconds)

请告诉我如何通过使用简单的 java 桌面应用程序访问这个 bean,因为使用 Java EE 应用程序客户端没有问题,但是相同的代码给出了这些错误

4

1 回答 1

1

您可能没有遵循如何从独立Java 客户端访问远程 EJB(部署在 GlassFish 服务器上)的说明。这并不完全是微不足道的。

这应该回答你的问题。一般来说,我怀疑您缺少的是gf-client.jar类路径中的文件。请按照说明告诉我们:

http://glassfish.java.net/javaee5/ejb/EJB_FAQ.html#StandaloneRemoteEJB

于 2012-11-23T01:40:35.293 回答