jndi快速上手
2007-04-20 15:20
141 查看
新看到的一个对jndi初学比较好的例子
package jndi;
import javax.naming.*;
import java.util.Hashtable;
class JNDI {
static Context ctx = null;
public JNDI()
{ }
//将对象object绑定到WebLogic Server的名字服务中
public static void bind(String name, String object) {
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
try {
ctx = new InitialContext(ht);
ctx.rebind(name, object);
}
catch (NamingException e) {
System.out.println(e);
}
finally {
try {
ctx.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
//通过JNDI查询指定的对象
public static Object lookUp(String name) {
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
try {
ctx = new InitialContext(ht);
Object object = ctx.lookup(name);
return object;
}
catch (NamingException e) {
System.out.println(e);
}
finally {
try {
ctx.close();
}
catch (Exception e) {
System.out.println(e);
}
}
return null;
}
public static void main(String args[]) {
String arg = args[0];
if(arg.equals("bind")) {
System.out.println("bind begin...");
String bookName = "WebLogic introduction";
bind("bookname", bookName);
System.out.println("bind end");
}
if(arg.equals("lookup")) {
System.out.println("lookup begin...");
String bookName;
bookName = (String)lookUp("bookname");
System.out.println("bookname is: "+bookName);
System.out.println("lookup end");
}
}
}
要注意的是,如果在运行时出现如下错误(BEA weblogic 8)
javax.naming.NoInitialContextException: Cannot instantiate class: weblogic.jndi.WLInitialContextFactory [Root exception is java.lang.ClassNotFoundException: weblogic.jndi.WLInitialContextFactory]” 为什么编译没问题了,却还是出错呢,这是网络上问得最多的问题。这是缺少"\bea\weblogic81\server\lib\wlclient.jar"文件所致。 在buildpath里加入这个包后再运行。
如果还是出错,显示“Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/corba/se/connection/ORBSocketFactory” 还是缺少包,这时如果你只安装有jre1.5.*,那是没有ORBSocketFactory这个类文件的。还是找个jre1.4.*吧,这个类在“jre/lib/rt.jar”包中。据说这个问题在weblogic9中已解决
package jndi;
import javax.naming.*;
import java.util.Hashtable;
class JNDI {
static Context ctx = null;
public JNDI()
{ }
//将对象object绑定到WebLogic Server的名字服务中
public static void bind(String name, String object) {
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
try {
ctx = new InitialContext(ht);
ctx.rebind(name, object);
}
catch (NamingException e) {
System.out.println(e);
}
finally {
try {
ctx.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
//通过JNDI查询指定的对象
public static Object lookUp(String name) {
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
try {
ctx = new InitialContext(ht);
Object object = ctx.lookup(name);
return object;
}
catch (NamingException e) {
System.out.println(e);
}
finally {
try {
ctx.close();
}
catch (Exception e) {
System.out.println(e);
}
}
return null;
}
public static void main(String args[]) {
String arg = args[0];
if(arg.equals("bind")) {
System.out.println("bind begin...");
String bookName = "WebLogic introduction";
bind("bookname", bookName);
System.out.println("bind end");
}
if(arg.equals("lookup")) {
System.out.println("lookup begin...");
String bookName;
bookName = (String)lookUp("bookname");
System.out.println("bookname is: "+bookName);
System.out.println("lookup end");
}
}
}
要注意的是,如果在运行时出现如下错误(BEA weblogic 8)
javax.naming.NoInitialContextException: Cannot instantiate class: weblogic.jndi.WLInitialContextFactory [Root exception is java.lang.ClassNotFoundException: weblogic.jndi.WLInitialContextFactory]” 为什么编译没问题了,却还是出错呢,这是网络上问得最多的问题。这是缺少"\bea\weblogic81\server\lib\wlclient.jar"文件所致。 在buildpath里加入这个包后再运行。
如果还是出错,显示“Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/corba/se/connection/ORBSocketFactory” 还是缺少包,这时如果你只安装有jre1.5.*,那是没有ORBSocketFactory这个类文件的。还是找个jre1.4.*吧,这个类在“jre/lib/rt.jar”包中。据说这个问题在weblogic9中已解决
相关文章推荐
- jndi快速上手
- jndi快速上手
- 快速上手Spring--6. Singleton的使用与否
- 2.SQLite3 快速上手
- 快速 上手centos7命令
- 快速上手——我用scrapy写爬虫(一)
- 学一点Git--20分钟git快速上手
- 快速上手Spring--9. Lookup方法注入
- 快速上手Spring--4.安装和使用SpringIDE
- 快速上手Spring--1.收集的一些资料
- logstash5.x 快速上手教程
- .Net程序员玩转Android系列之三~快速上手
- Eclipse快速上手Hibernate之入门实例
- ofbiz 快速上手实例
- JDOM处理XML快速上手
- 快速上手Gobject
- 程序员如何快速上手一个自己不太熟悉的新项目
- Eclipse快速上手指南之使用JUnit
- 快速上手RaphaelJS--RaphaelJS_Starter翻译(三)
- 第 1 章 在Tomcat中快速上手