照着书上的例子竟然运行不了,抛NoInitialContextException异常,是否要用到hashtable?
要怎么写呢?
import javax.naming.*;public class JNDILookup {
public final static String JNDI="sams/book";
public static void main(String[] args) {
try{
Context ic=new InitialContext();
String name=(String)ic.lookup(JNDI);
System.out.println(JNDI+"="+name);
}catch(NamingException e){
System.err.println(e);
System.exit(1);
}catch(ClassCastException e){
System.err.println(e);
System.exit(1);
}
}
}

解决方案 »

  1.   

    public final static String JNDI="sams/book";
    ----------------------------------------------------
    以上代码,需要根据自己的实际情况来设置JNDI
      

  2.   

    aywrenyue(基本功修炼中……) 能仔细地给我讲一下吗?我看了好几本书了,一直解决不了这个问题!
      

  3.   

    public final static String JNDI="sams/book";你要看下你的JNDI是不是已经配置好啊."sams/book"是这个指向吗??
      

  4.   

    哎,咋这么晕呢?书上都没说啊!
    Hashtable ht = new Hashtable();
    ht.put(Context.INITIAL_CONTEXT_FACTORY,
    "com.sun.jndi.rmi.registry.RegistryContextFactory");
    ht.put(Context.PROVIDER_URL, "rmi://localhost:1099");是这些吗?
      

  5.   

    public static Hashtable getEnvironment(){
            Hashtable h=new Hashtable();
            h.putAll(System.getProperties());
            
            String url="rmi://localhost:1099";      
            if(url!=null){
             h.put(InitialContext.PROVIDER_URL,url);
            }
            
            String fac="com.sun.jndi.rmi.registry.RegistryContextFactory";
            if(fac!=null){
             h.put(InitialContext.INITIAL_CONTEXT_FACTORY,fac);
            }        
            
            return h;
        }
      

  6.   

    javax.naming.ServiceUnavailableException [Root exception is java.rmi.ConnectException: Connection refused to host: localhost; nested exception is: 
    java.net.ConnectException: Connection refused: connect]超级郁闷啊!