不知道你用的时候什么 部属服务器。要设置一些属性值
weblogic的:
private InitialContext getContext() throws NamingException {
Hashtable props = new Hashtable();props.put(
InitialContext.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
props.put(InitialContext.PROVIDER_URL, "t3://127.0.0.1:7001");// This establishes the security for authorization/authentication
props.put(InitialContext.SECURITY_PRINCIPAL,"username");
props.put(InitialContext.SECURITY_CREDENTIALS,"password");InitialContext initialContext = new InitialContext(props);
return initialContext;
}

解决方案 »

  1.   

    jboss的。Hashtable env = new Hashtable();
      env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
      env.put(Context.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( "HelloWorld" );
       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() );
      }
      

  2.   

    我用的是weblogic,我马上按照你的方法试试看,先谢了
      

  3.   

    我这样写的
    public class ConverterClient {    public static void main(String[] args) throws Exception{
            try {
                Context initial = new InitialContext();
                
                Properties environment= null;
                environment.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                                   "weblogic.jndi.WLInitialContextFactory");
                environment.setProperty(Context.PROVIDER_URL, "t3://localhost:7001");
                environment.put(InitialContext.SECURITY_PRINCIPAL,"weblogic");
               environment.put(InitialContext.SECURITY_CREDENTIALS,"PASSWORD");
                initial=new InitialContext(environment);
                Object objref = initial.lookup("MyConverter");
                HelloHome home =
                        (HelloHome) PortableRemoteObject.narrow(objref,
                        HelloHome.class);            Hello currencyConverter = home.create();            double amount = currencyConverter.dollarToYen(100.00);
                System.out.println(amount);
                amount = currencyConverter.yenToEuro(100.00);
                System.out.println(amount);            currencyConverter.remove();        } catch (Exception ex) {
                System.err.println("Caught an unexpected exception!");
                ex.printStackTrace();
            }
        }
    }
      

  4.   

    问题出现了,当运行到environment.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                                   "weblogic.jndi.WLInitialContextFactory");
    的时候就抛出了NullPointerException空指针的异常,我晕了
      

  5.   

    你这样写:
    public static void main(String[] args) throws Exception{
            try {
                Context initial = new InitialContext();
                
                properties = new Properties();
                environment.put(Context.INITIAL_CONTEXT_FACTORY,
                                   "weblogic.jndi.WLInitialContextFactory");
                environment.put(Context.PROVIDER_URL, "t3://localhost:7001");
                environment.put(InitialContext.SECURITY_PRINCIPAL,"weblogic");
               environment.put(InitialContext.SECURITY_CREDENTIALS,"PASSWORD");
                initial=new InitialContext(environment);
      

  6.   

    environment 改成properties 前面注意声明
      

  7.   

    我这样写了之后就没报错了:
    public class ConverterClient {    public static void main(String[] args) throws Exception {
            try {
                   InitialContext initial= getContext();
               Object objref = initial.lookup("MyConverter");
                HelloHome home =
                        (HelloHome) PortableRemoteObject.narrow(objref,
                        HelloHome.class);            Hello currencyConverter = home.create();            double amount = currencyConverter.dollarToYen(100.00);
                System.out.println(amount);
                amount = currencyConverter.yenToEuro(100.00);
                System.out.println(amount);            currencyConverter.remove();        } catch (Exception ex) {
                System.err.println("Caught an unexpected exception!");
                ex.printStackTrace();
            }
        }    private static InitialContext getContext() throws NamingException {
            Hashtable props = new Hashtable();        props.put(
                    InitialContext.INITIAL_CONTEXT_FACTORY,
                    "weblogic.jndi.WLInitialContextFactory");
            props.put(InitialContext.PROVIDER_URL, "t3://127.0.0.1:7001");// This establishes the security for authorization/authentication
            props.put(InitialContext.SECURITY_PRINCIPAL, "weblogic");
            props.put(InitialContext.SECURITY_CREDENTIALS, "PASSWORD");        InitialContext initialContext = new InitialContext(props);
            return initialContext;
        }}