客户端程序为什么要和ejb打包在一起?客户端程序应该是调用 ejb而已, 放在哪里都可以的。比如已经部署了一个叫做 helloword的ejb 这样我只要在jndi上拿到这个ejb的home接口 再create一个本地接口 就可以调用ejb session bean的方法了。 示例程序大概是这样。public class TestEjb{
    public static void main(String args[]) {
             try {
InitialContext context = new InitialContext();
hellowordHome home = (hellowordHome ) context.lookup("helloword");
                           hellowordRemote remote = home.create();
                           remote.callBusinessMethod();// 调用ejb的方法
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}    
             }}

解决方案 »

  1.   

    那么我启动了jboss服务器之后,怎么运行这个客户程序呢,是和普通的java程序运行方式一样吗.
      

  2.   

    Java_caicainiao 的回答让我茅塞顿开,但是我在eclispe中运行客户程序的时候报错了.能帮忙看一下吗?多谢了
    我的客户程序如下:
    package sample;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import java.util.Hashtable;public class HelloWorldClient{
    public static void main( String [] args ){
    Hashtable env = new Hashtable();
    env.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
    env.put("java.naming.provider.url", "localhost:1099");
    env.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
    try{
    Context ctx = new InitialContext(env);
    Object obj = ctx.lookup( "HelloWord" );
    HelloWorldHome home =(HelloWorldHome)javax.rmi.PortableRemoteObject.narrow(obj, HelloWorldHome.class );
    HelloWorld helloWorld = home.create();
    System.out.println( helloWorld.hello());
    helloWorld.remove();
    }
    catch ( Exception e )
    {
    e.printStackTrace();
    System.out.println( "Exception: " + e.getMessage() );
    }
    }
    } 出错信息如下:
    Exception in thread "main" java.lang.NoClassDefFoundError: org/jboss/logging/Logger
    at org.jnp.interfaces.NamingContext.<clinit>(NamingContext.java:158)
    at org.jnp.interfaces.NamingContextFactory.getInitialContext(NamingContextFactory.java:56)
    at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
    at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.init(Unknown Source)
    at javax.naming.InitialContext.<init>(Unknown Source)
    at sample.HelloWorldClient.main(HelloWorldClient.java:13)这是什么原因呢?
      

  3.   

    从抛出的异常来看 是你没有把jboss的logger类加到classpath下, 尝试在jboss文件夹的lib里找到这个类所在的jar包 加到  classpath下就好了
      

  4.   

    PS: 这个包是jboss-common.jar 在lib文件夹下
      

  5.   

    我刚才试了,但是却很奇怪还是不行.现在报的错是这样的,能再帮忙我看看吗?多谢了.javax.naming.NameNotFoundException: HelloWord not bound
    at org.jnp.server.NamingServer.getBinding(NamingServer.java:529)
    at org.jnp.server.NamingServer.getBinding(NamingServer.java:537)
    at org.jnp.server.NamingServer.getObject(NamingServer.java:543)
    at org.jnp.server.NamingServer.lookup(NamingServer.java:296)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:294)
    at sun.rmi.transport.Transport$1.run(Transport.java:153)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:149)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:460)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:701)
    at java.lang.Thread.run(Thread.java:595)
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
    at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
    at sun.rmi.server.UnicastRef.invoke(Unknown Source)
    at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
    at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:625)
    at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:587)
    at javax.naming.InitialContext.lookup(Unknown Source)
    at sample.HelloWorldClient.main(HelloWorldClient.java:14)
    Exception: HelloWord not bound这个错误是什么造成的呢?
      

  6.   

    要运行Ejb,一帮都需要些什么jar文件呢?
      

  7.   

    呵呵 这个异常说的是HelloWord这个ejb bean没有绑定到jndi上, 首先确定你的ejb是不是部署成功了 然后检查你ejb bean的名字是不是错了. 在启动jboss的dos窗口里打出的信息里查查看有没有HelloWord这个bean
      

  8.   

    有没有导入j2ee里面的j2ee.jar和jbossall-client.jar两个jar文件编译啊!!
      

  9.   

    我把我的程序帖出来能帮我看看吗?多谢了.home接口:
    package sample;
    import java.rmi.*;
    import javax.ejb.*;public interface HelloWorldHome extends EJBHome{

    HelloWorld create() throws RemoteException,CreateException;}remote接口:
    package sample;
    import java.rmi.*;
    import javax.ejb.*;public interface HelloWorld extends EJBObject{

    public String hello() throws RemoteException;}bean实现类:
    package sample;import java.rmi.RemoteException;import javax.ejb.EJBException;
    import javax.ejb.SessionBean;
    import javax.ejb.SessionContext;public class HelloWorldBean implements SessionBean{

    private SessionContext ctx;

    public void setSessionContext(SessionContext ctx){
    this.ctx = ctx;
    }

    public void ejbCreate(){
    System.out.println("ejbCreate()");
    }

    public void ejbRemove(){
    System.out.println("ejbRemove()");
    }

    public void ejbActivate(){
    System.out.println("ejbActivate()");
    }

    public void ejbPassivate(){
    System.out.println("ejbPassivate()");
    }

    public String hello(){
    System.out.println("hello()");
    return "hello,world";
    }
    } 客户程序:
    package sample;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import java.util.Hashtable;public class HelloWorldClient{
    public static void main( String [] args ){
    Hashtable env = new Hashtable();
    env.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
    env.put("java.naming.provider.url", "localhost:1099");
    env.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
    try{
    Context ctx = new InitialContext(env);
    Object obj = ctx.lookup( "ejb/HelloWord" );
    HelloWorldHome home =(HelloWorldHome)javax.rmi.PortableRemoteObject.narrow(obj, HelloWorldHome.class );
    HelloWorld helloWorld = home.create();
    System.out.println( helloWorld.hello());
    helloWorld.remove();
    }
    catch ( Exception e )
    {
    e.printStackTrace();
    System.out.println( "Exception: " + e.getMessage() );
    }
    }
    } ejb-jar.xml:
    <?xml version="1.0" encoding="UTF-8"?>  
    <!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN' 'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>  <ejb-jar>
         <description>JBoss Hello World Application</description>
         <display-name>Hello World EJB</display-name>
         <enterprise-beans>
           <session>
             <ejb-name>HelloWorld</ejb-name>
             <home>sample.HelloWorldHome</home>
             <remote>sample.HelloWorld</remote>
             <ejb-class>sample.HelloWorldBean</ejb-class>
             <session-type>Stateless</session-type>
             <transaction-type>Bean</transaction-type>
           </session>
         </enterprise-beans>
    </ejb-jar>jboss.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <?xml version="1.0" encoding="UTF-8"?>
    <jboss>
    <enterprise-beans>
    <session>
    <ejb-name>HelloWorld</ejb-name>
    <jidi-name>ejb/HelloWord</jidi-name>
    </session>
    </enterprise-beans>
    </jboss>能帮忙看看吗?我哪里写的不对,多谢了!!
      

  10.   

    我加入了j2ee.jar和jbossall-client.jar两个jar文件,但是还是出现如下这个错误啊:Exception: ejb not bound
    javax.naming.NameNotFoundException: ejb not bound
    at org.jnp.server.NamingServer.getBinding(NamingServer.java:529)
    at org.jnp.server.NamingServer.getBinding(NamingServer.java:537)
    at org.jnp.server.NamingServer.getObject(NamingServer.java:543)
    at org.jnp.server.NamingServer.lookup(NamingServer.java:267)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:585)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:294)
    at sun.rmi.transport.Transport$1.run(Transport.java:153)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:149)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:460)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:701)
    at java.lang.Thread.run(Thread.java:595)
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
    at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
    at sun.rmi.server.UnicastRef.invoke(Unknown Source)
    at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
    at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:625)
    at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:587)
    at javax.naming.InitialContext.lookup(Unknown Source)
    at sample.HelloWorldClient.main(HelloWorldClient.java:14)
      

  11.   

    在http://localhost:8080/jmx-console下查看你的EJB部署成功了没有,也就是绑定成功了没有,在控制台下也可以看到.如果成功了,那还有原因可能就是你的配置文件有问题,通过jndi找不到响应的EJB.
      

  12.   

    后台好像成功了啊,下面是后台信息.22:09:04,718 INFO  [EjbModule] Deploying HelloWorld
    22:09:05,529 INFO  [ProxyFactory] Bound EJB Home 'HelloWorld' to jndi 'HelloWorl
    d'
    22:09:05,539 INFO  [EJBDeployer] Deployed: file:/D:/jboss-4.0.4.GA/jboss-4.0.4.G
    A/server/default/deploy/HelloWord.jar
      

  13.   

    你的java程序能编译通过么
    javac -cp **/**/j2ee.jar;**/**/jbossall-client.jar HelloWorldClient.java
    java -cp **/**/j2ee.jar;**/**/jbossall-client.jar HelloWorldClient
    **是你上面两个文件所在的目录
      

  14.   

    肯定不是编译的问题,在lookup的时候用"ejb/HelloWord"试一下。
      

  15.   

    已经成功编译通过了,就是运行的时候总是 ejb not bound
    能告诉我运行这个例子都需要些什么jar包吗?
      

  16.   

    不行就用"HelloWord" lookup一下
      

  17.   

    我用HelloWord也不行,我把我的源文件和配置文件都帖在上面了,能帮忙看看吗?多谢了.
      

  18.   

    Object obj = ctx.lookup( "ejb/HelloWord" );
    好象写错了吧,是不是写成("HelloWorld");
      

  19.   

    看到一个拼写错误 不知道你注意到没有 jboss.xml 文件里 你的<jidi-name>ejb/HelloWord</jidi-name> 应该是<jndi-name>吧
      

  20.   

    是拼写错误吧? 呵呵。 祝贺你第一个j2ee例子成功运行。 这是个不错的开始。By the way,现在ejb3 + jboss4.0.4中已经用annotation代替了原来繁杂的配置文件编写部署ejb已经变得很容易了 可以到jboss官方网站www.jboss.org上了解跟多情况。最后祝你技术进步, 工作学习顺利:)