只有在EJB中找一个ejb时。Context initial = new InitialContext();
这样是可以找到的,
除此之外,必须另有
  private static Context getInitialContext() throws NamingException
  {
     if(locale){
      return new InitialContext();
     }else{
      Properties properties = new Properties();
      properties.put("java.naming.factory.initial", "weblogic.jndi.WLInitialContextFactory");
      properties.put("java.naming.provider.url", EJBSERVER_URL);
      return new InitialContext(properties);
     }
  }
即java.naming.factory.initial和java.naming.provider.url这两个参数必须有,
没有,机器到什么好方去找你的ejb呢。
但在服务器上,如EJB中,找另一个ejb,机子会自动去找。
:)

解决方案 »

  1.   

    getInitialContext()是谁的方法,谁调用执行的,在j2ee中他的一个参数可以是“weblogic.jndi.WLInitialContextFactory”吗?EJBSERVER_URL要不要写端口?
    这端代码怎么加到上述程序中呢?!!!
      

  2.   

    如果class在同一个package下,就不用import
      

  3.   

    为什么不用JBuilder来做呢,这些代码Jbuilder会自己加进去啊,应该很简单的阿。
    下面就是我做的,在Jbuilder6下运行通过
    远程接口:Teller.javapackage project20;import javax.ejb.*;
    import java.util.*;
    import java.rmi.*;public interface Teller extends javax.ejb.EJBObject {
      public double Add(double from, double to) throws RemoteException;
      public void setName(java.lang.String name) throws RemoteException;
      public java.lang.String getName() throws RemoteException;
    }
    本地接口:TellerHome.javapackage project20;import javax.ejb.*;
    import java.util.*;
    import java.rmi.*;public interface TellerHome extends javax.ejb.EJBHome {
      public Teller create() throws CreateException, RemoteException;
    }企业bean(statless):TellerBeanpackage project20;import javax.ejb.*;public class TellerBean implements SessionBean {  SessionContext sessionContext;
      java.lang.String name;  public void ejbCreate() throws CreateException {
      }  public void ejbRemove() {
      }  public void ejbActivate() {
      }  public void ejbPassivate() {
      }  public void setSessionContext(SessionContext sessionContext) {
        this.sessionContext = sessionContext;
      }
      public double Add(double from, double to) {
        return from+to;
      }
      public java.lang.String getName() {
        return name;
      }  public void setName(java.lang.String name) {
        this.name = name;
      }
    }
    客户端程序TellerTestClient2.javapackage project20;import javax.naming.*;
    import java.util.Properties;
    import javax.rmi.PortableRemoteObject;public class TellerTestClient2 {
      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 TellerHome tellerHome = null;
      private Teller teller = null;  //Construct the EJB test client
      public TellerTestClient2() {
        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("Teller");      //cast to Home interface
          tellerHome = (TellerHome) PortableRemoteObject.narrow(ref, TellerHome.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 Teller create() {
        long startTime = 0;
        if (logging) {
          log("Calling create()");
          startTime = System.currentTimeMillis();
        }
        try {
          teller = tellerHome.create();
          if (logging) {
            long endTime = System.currentTimeMillis();
            log("Succeeded: create()");
            log("Execution time: " + (endTime - startTime) + " ms.");
          }
        }
        catch(Exception e) {
          if (logging) {
            log("Failed: create()");
          }
          e.printStackTrace();
        }    if (logging) {
          log("Return value from create(): " + teller + ".");
        }
        return teller;
      }  //----------------------------------------------------------------------------
      // Methods that use Remote interface methods to access data through the bean
      //----------------------------------------------------------------------------  public double Add(double from, double to) {
        double returnValue = 0f;
        if (teller == null) {
          System.out.println("Error in Add(): " + ERROR_NULL_REMOTE);
          return returnValue;
        }
        long startTime = 0;
        if (logging) {
          log("Calling Add(" + from + ", " + to + ")");
          startTime = System.currentTimeMillis();
        }    try {
          returnValue = teller.Add(from, to);
          if (logging) {
            long endTime = System.currentTimeMillis();
            log("Succeeded: Add(" + from + ", " + to + ")");
            log("Execution time: " + (endTime - startTime) + " ms.");
          }
        }
        catch(Exception e) {
          if (logging) {
            log("Failed: Add(" + from + ", " + to + ")");
          }
          e.printStackTrace();
        }    if (logging) {
          log("Return value from Add(" + from + ", " + to + "): " + returnValue + ".");
        }
        return returnValue;
      }  public void setName(String name) {
        if (teller == null) {
          System.out.println("Error in setName(): " + ERROR_NULL_REMOTE);
          return ;
        }
        long startTime = 0;
        if (logging) {
          log("Calling setName(" + name + ")");
          startTime = System.currentTimeMillis();
        }    try {
          teller.setName(name);
          if (logging) {
            long endTime = System.currentTimeMillis();
            log("Succeeded: setName(" + name + ")");
            log("Execution time: " + (endTime - startTime) + " ms.");
          }
        }
        catch(Exception e) {
          if (logging) {
            log("Failed: setName(" + name + ")");
          }
          e.printStackTrace();
        }
      }  public String getName() {
        String returnValue = "";
        if (teller == null) {
          System.out.println("Error in getName(): " + ERROR_NULL_REMOTE);
          return returnValue;
        }
        long startTime = 0;
        if (logging) {
          log("Calling getName()");
          startTime = System.currentTimeMillis();
        }    try {
          returnValue = teller.getName();
          if (logging) {
            long endTime = System.currentTimeMillis();
            log("Succeeded: getName()");
            log("Execution time: " + (endTime - startTime) + " ms.");
          }
        }
        catch(Exception e) {
          if (logging) {
            log("Failed: getName()");
          }
          e.printStackTrace();
        }    if (logging) {
          log("Return value from getName(): " + 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) {
        TellerTestClient2 client = new TellerTestClient2();
        client.create();
        double total;
        total=client.Add(10,20);
        System.out.println("total:"+total);
        client.setName("Jack");
        System.out.println("name:"+client.getName());
        // Use the client object to call one of the Home interface wrappers
        // above, to create a Remote interface reference to the bean.
        // If the return value is of the Remote interface type, you can use it
        // to access the remote interface methods.  You can also just use the
        // client object to call the Remote interface wrappers.
      }
    }
      

  4.   

    不想用weblogic、T3协议,多谢你了