照着书上的例子竟然运行不了,抛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);
}
}
}
要怎么写呢?
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);
}
}
}
----------------------------------------------------
以上代码,需要根据自己的实际情况来设置JNDI
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.rmi.registry.RegistryContextFactory");
ht.put(Context.PROVIDER_URL, "rmi://localhost:1099");是这些吗?
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;
}
java.net.ConnectException: Connection refused: connect]超级郁闷啊!