照着书上的例子竟然运行不了

cyrixIII 2006-03-29 09:52:28
照着书上的例子竟然运行不了,抛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);
}
}
}
...全文
158 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
cyrixIII 2006-03-29
  • 打赏
  • 举报
回复
javax.naming.ServiceUnavailableException [Root exception is java.rmi.ConnectException: Connection refused to host: localhost; nested exception is:
java.net.ConnectException: Connection refused: connect]

超级郁闷啊!
piaopiao11 2006-03-29
  • 打赏
  • 举报
回复
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;
}
cyrixIII 2006-03-29
  • 打赏
  • 举报
回复
哎,咋这么晕呢?书上都没说啊!
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.rmi.registry.RegistryContextFactory");
ht.put(Context.PROVIDER_URL, "rmi://localhost:1099");

是这些吗?
jackson416 2006-03-29
  • 打赏
  • 举报
回复
public final static String JNDI="sams/book";

你要看下你的JNDI是不是已经配置好啊."sams/book"是这个指向吗??
cyrixIII 2006-03-29
  • 打赏
  • 举报
回复
aywrenyue(基本功修炼中……) 能仔细地给我讲一下吗?我看了好几本书了,一直解决不了这个问题!
aywrenyue 2006-03-29
  • 打赏
  • 举报
回复
public final static String JNDI="sams/book";
----------------------------------------------------
以上代码,需要根据自己的实际情况来设置JNDI
cyrixIII 2006-03-29
  • 打赏
  • 举报
回复
怎么没有人回答我呢?

62,624

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧