//TestSes.java
package testses;import javax.ejb.*;
import java.util.*;
import java.rmi.*;public interface TestSes extends javax.ejb.EJBObject {
  public String getName() throws RemoteException;
}
//TestSesBean.java
package testses;import javax.ejb.*;public class TestSesBean implements SessionBean {
  SessionContext sessionContext;
  public void ejbCreate() throws CreateException {
    /**@todo Complete this method*/
  }
  public void ejbRemove() {
    /**@todo Complete this method*/
  }
  public void ejbActivate() {
    /**@todo Complete this method*/
  }
  public void ejbPassivate() {
    /**@todo Complete this method*/
  }
  public void setSessionContext(SessionContext sessionContext) {
    this.sessionContext = sessionContext;
  }
  public String getName() {
    return "Testing Successful"; //change null to “Testing Successful”
  }
}
//TestSesHome.java
package testses;import javax.ejb.*;
import java.util.*;
import java.rmi.*;public interface TestSesHome extends javax.ejb.EJBHome {
  public TestSes create() throws CreateException, RemoteException;
}
//TestSesTestClient.java
package testses;import javax.naming.*;
import java.util.Properties;
import javax.rmi.PortableRemoteObject;
public class TestSesTestClient
    extends Object {
  private static TestSesHome testSesHome = null;  //Construct the EJB test client
  public TestSesTestClient() {
    initialize();
  }  public void initialize() {
    try {
      //get naming context
      Context context = getInitialContext();      //look up jndi name
      Object ref = context.lookup("TestSes");
      //look up jndi name and cast to Home interface
      testSesHome = (TestSesHome) PortableRemoteObject.narrow(ref, TestSesHome.class);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }  private Context getInitialContext() throws Exception {
    String url = "t3://192.168.0.3: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) {
      System.out.println("Unable to connect to WebLogic server at " + url);
      System.out.println("Please make sure that the server is running.");
      throw e;
    }
  }  //----------------------------------------------------------------------------
  // Utility Methods
  //----------------------------------------------------------------------------  public TestSesHome getHome() {
    return testSesHome;
  }  //Main method  public static void main(String[] args) {
    TestSesTestClient client = new TestSesTestClient();
    // Use the getHome() method of the client object to call Home interface
    // methods that will return a Remote interface reference.  Then
    // use that Remote interface reference to access the EJB.
    try {
      TestSes ts =testSesHome.create();
      String name = ts.getName();
      System.out.println("Name from the Test Client = " + name);
    }
    catch (Exception ex) {}  }
}

解决方案 »

  1.   

    但我将
    try {
          client.create();//这里报错
          String name = client.getName();//这里报错
          System.out.println("Name from the Test Client = " + name);
        }
        catch (Exception ex) {}
    ---------------------------------------------------------------------------
    /*以上代码是Borland® JBuilder® 7 and BEA
     *WebLogic Server™ 7.0 Integration 中的例子*/
    ------------------------------------------------------------------------------
    改为这样就可以编译通过了
     try {
          TestSes ts =testSesHome.create();
          String name = ts.getName();
          System.out.println("Name from the Test Client = " + name);
        }
        catch (Exception ex) {}请大虾给看看.