相关的文件代码如下:
Count.java:
package count;import javax.ejb.*;
import java.util.*;
import java.rmi.*;public interface Count extends javax.ejb.EJBObject {
  public int Count() throws RemoteException;
}CountBean.java:
package count;import javax.ejb.*;public class CountBean implements SessionBean {  SessionContext sessionContext;
  public int val;  public void ejbCreate(int val) throws CreateException {
    this.val =val;
    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 void setSessionContext(SessionContext sessionContext) {
    this.sessionContext = sessionContext;
  }  public int Count()
  {
    System.out.println("Count()") ;
    return ++val;
  }}CountHome.java:
package count;import javax.ejb.*;
import java.util.*;
import java.rmi.*;public interface CountHome extends javax.ejb.EJBHome {
  public Count create(int val) throws CreateException, RemoteException;
}

解决方案 »

  1.   

    在JBUILDER中自动生成的EJBTESTCLIENT如下:(CountClient.java)
    package count;import javax.naming.*;
    import java.util.Properties;
    import javax.rmi.PortableRemoteObject;
    import javax.ejb.*;
    public class CountClient {
      private static final String ERROR_NULL_REMOTE = "Remote interface reference is null.  It must be created by calling one of the Home interface methods first.";
      private static final int MAX_OUTPUT_LINE_LENGTH = 100;
      private boolean logging = true;
      private CountHome countHome = null;
      private Count count = null;  //Construct the EJB test client
      public CountClient() {
        long startTime = 0;
        if (logging) {
          log("Initializing bean access.");
          startTime = System.currentTimeMillis();
        }    try {
          //get naming context
          Context ctx = getInitialContext();      //look up jndi name
          Object ref = ctx.lookup("Count");      //cast to Home interface
          countHome = (CountHome) PortableRemoteObject.narrow(ref, CountHome.class);
          if (logging) {
            long endTime = System.currentTimeMillis();
            log("Succeeded initializing bean access.");
            log("Execution time: " + (endTime - startTime) + " ms.");
          }
        }
        catch(Exception e) {
          if (logging) {
            log("Failed initializing bean access.");
          }
          e.printStackTrace();
        }
      }  private Context getInitialContext() throws Exception {
        String url = "t3://localhost:7001";
        String user = null;
        String password = null;
        Properties properties = null;
        try {
          properties = new Properties();
          properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
          properties.put(Context.PROVIDER_URL, url);
          if (user != null) {
            properties.put(Context.SECURITY_PRINCIPAL, user);
            properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);
          }      return new InitialContext(properties);
        }
        catch(Exception e) {
          log("Unable to connect to WebLogic server at " + url);
          log("Please make sure that the server is running.");
          throw e;
        }
      }  //----------------------------------------------------------------------------
      // Methods that use Home interface methods to generate a Remote interface reference
      //----------------------------------------------------------------------------  public Count create(int val) {
        long startTime = 0;
        if (logging) {
          log("Calling create(" + val + ")");
          startTime = System.currentTimeMillis();
        }
        try {
          count = countHome.create(val);
          if (logging) {
            long endTime = System.currentTimeMillis();
            log("Succeeded: create(" + val + ")");
            log("Execution time: " + (endTime - startTime) + " ms.");
          }
        }
        catch(Exception e) {
          if (logging) {
            log("Failed: create(" + val + ")");
          }
          e.printStackTrace();
        }    if (logging) {
          log("Return value from create(" + val + "): " + count + ".");
        }
        return count;
      }  //----------------------------------------------------------------------------
      // Methods that use Remote interface methods to access data through the bean
      //----------------------------------------------------------------------------  public int Count() {
        int returnValue = 0;
        if (count == null) {
          System.out.println("Error in Count(): " + ERROR_NULL_REMOTE);
          return returnValue;
        }
        long startTime = 0;
        if (logging) {
          log("Calling Count()");
          startTime = System.currentTimeMillis();
        }    try {
          returnValue = count.Count();
          if (logging) {
            long endTime = System.currentTimeMillis();
            log("Succeeded: Count()");
            log("Execution time: " + (endTime - startTime) + " ms.");
          }
        }
        catch(Exception e) {
          if (logging) {
            log("Failed: Count()");
          }
          e.printStackTrace();
        }    if (logging) {
          log("Return value from Count(): " + returnValue + ".");
        }
        return returnValue;
      }  //----------------------------------------------------------------------------
      // Utility Methods
      //----------------------------------------------------------------------------  private void log(String message) {
        if (message == null) {
          System.out.println("-- null");
          return ;
        }
        if (message.length() > MAX_OUTPUT_LINE_LENGTH) {
          System.out.println("-- " + message.substring(0, MAX_OUTPUT_LINE_LENGTH) + " ...");
        }
        else {
          System.out.println("-- " + message);
        }
      }
      //Main method  public static void main(String[] args) {
        CountClient client = new CountClient();
        
      }
    }
      

  2.   

    代码太多了,看的头大,
    你的这个BEAN是从别的地方下载的吧?
    你没必要把那个CLIENT弄到JB6中去
    你可以在命令行编译他们的
    你把REMOTE、HOME、EJB、CLIENT这几个文件放到同一个目录中
    用:
    javac -d . -classpath 你的CLASSPATH(最好把weblogic.jar写上) *.java
    就可以编译成功了,运行CLIENT
    java 包名.你的CLIENT的CLASS名
    你就可以看到结果了
      

  3.   

    部知道对不对!
    package count;import javax.naming.*;
    import java.util.Properties;
    import javax.rmi.PortableRemoteObject;
    import javax.ejb.*;
    public class CountClient {
      private static final String ERROR_NULL_REMOTE = "Remote interface reference is null.  It must be created by calling one of the Home interface methods first.";
      private static final int MAX_OUTPUT_LINE_LENGTH = 100;
      private boolean logging = true;
      private CountHome countHome = null;
      private Count count = null;  //Construct the EJB test client
      public CountClient() {
        long startTime = 0;
        if (logging) {
          log("Initializing bean access.");
          startTime = System.currentTimeMillis();
        }
      }
    /************************************///add the method
    public CountHome getCountHome() throws Exception
    {
     try {
          //get naming context
          Context ctx = getInitialContext();      //look up jndi name
          Object ref = ctx.lookup("Count");      //cast to Home interface
          countHome = (CountHome) PortableRemoteObject.narrow(ref, CountHome.class);
          if (logging) {
            long endTime = System.currentTimeMillis();
            log("Succeeded initializing bean access.");
            log("Execution time: " + (endTime - startTime) + " ms.");
          }
      return countHome;
        }
        catch(Exception e) {
          if (logging) {
            log("Failed initializing bean access.");
          }
          e.printStackTrace();
        }}
    /******************************************/
      private Context getInitialContext() throws Exception {
        String url = "t3://localhost:7001";
        String user = null;
        String password = null;
        Properties properties = null;
        try {
          properties = new Properties();
          properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
          properties.put(Context.PROVIDER_URL, url);
          if (user != null) {
            properties.put(Context.SECURITY_PRINCIPAL, user);
            properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);
          }      return new InitialContext(properties);
        }
        catch(Exception e) {
          log("Unable to connect to WebLogic server at " + url);
          log("Please make sure that the server is running.");
          throw e;
        }
      }  //----------------------------------------------------------------------------
      // Methods that use Home interface methods to generate a Remote interface reference
      //----------------------------------------------------------------------------  public Count create(int val) {
        long startTime = 0;
        if (logging) {
          log("Calling create(" + val + ")");
          startTime = System.currentTimeMillis();
        }
        try {
          count = countHome.create(val);
          if (logging) {
            long endTime = System.currentTimeMillis();
            log("Succeeded: create(" + val + ")");
            log("Execution time: " + (endTime - startTime) + " ms.");
          }
        }
        catch(Exception e) {
          if (logging) {
            log("Failed: create(" + val + ")");
          }
          e.printStackTrace();
        }    if (logging) {
          log("Return value from create(" + val + "): " + count + ".");
        }
        return count;
      }  //----------------------------------------------------------------------------
      // Methods that use Remote interface methods to access data through the bean
      //----------------------------------------------------------------------------  public int Count() {
        int returnValue = 0;
        if (count == null) {
          System.out.println("Error in Count(): " + ERROR_NULL_REMOTE);
          return returnValue;
        }
        long startTime = 0;
        if (logging) {
          log("Calling Count()");
          startTime = System.currentTimeMillis();
        }    try {
          returnValue = count.Count();
          if (logging) {
            long endTime = System.currentTimeMillis();
            log("Succeeded: Count()");
            log("Execution time: " + (endTime - startTime) + " ms.");
          }
        }
        catch(Exception e) {
          if (logging) {
            log("Failed: Count()");
          }
          e.printStackTrace();
        }    if (logging) {
          log("Return value from Count(): " + returnValue + ".");
        }
        return returnValue;
      }  //----------------------------------------------------------------------------
      // Utility Methods
      //----------------------------------------------------------------------------  private void log(String message) {
        if (message == null) {
          System.out.println("-- null");
          return ;
        }
        if (message.length() > MAX_OUTPUT_LINE_LENGTH) {
          System.out.println("-- " + message.substring(0, MAX_OUTPUT_LINE_LENGTH) + " ...");
        }
        else {
          System.out.println("-- " + message);
        }
      }
      //Main method  public static void main(String[] args) {
    CountClient client = new CountClient();
    CountHome=Home=client.getCountHome();
    Count count[]=new Count[3];
          int countVal=0;
          System.out.println("Instantiating Bean......") ;
          for (int i=0; i<3; i++)
          {
            count[i]=Home.create(countVal) ;
            countVal=count[i].Count();
            System.out.println(countVal);
            Thread.sleep(500) ;
           }       System.out.println("Calling count() on Bean......") ;
           for (int i=0; i<3; i++)
           {
             countVal=count[i].Count();
             System.out.println(countVal) ;
             Thread.sleep(500) ;
            }       for(int i=0; i<3; i++)
           {
             count[i].remove() ;
            }   } catch(Exception e)
         {
            e.printStackTrace();
         }    
      }
    }
      

  4.   

    MAIN函数改为如下就可以了:
    Count count[]=new Count[3];
          int countVal=0;
          System.out.println("Instantiating Bean......") ;
          for (int i=0; i<3; i++)
          {
            try
            {count[i]=client.create(countVal) ;
            countVal=count[i].Count();
            System.out.println(countVal);
            Thread.sleep(500) ;
            } catch(Exception e)
              {
                e.printStackTrace() ;
              }
           }
        System.out.println("Calling count() on Bean......") ;
        for (int i=0; i<3; i++)
        {
          try
          {countVal=count[i].Count();
          System.out.println(countVal) ;
          Thread.sleep(500) ;
          }
            catch(Exception e)
            {
              e.printStackTrace() ;
            }
        }    for(int i=0; i<3; i++)
        {
          try
          {count[i].remove() ;
          }
            catch(Exception e)
            {
              e.printStackTrace() ;
            }
        }非常感谢 pengji(彭乃超) 和uu_snow(薇薇) !