现在的位置: 首页 > 综合 > 正文

JNDI 的例子 JNDI 的例子

2018年05月23日 ⁄ 综合 ⁄ 共 1140字 ⁄ 字号 评论关闭

JNDI 的例子

JNDI(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API。命名服务将名称和对象联系起来,使得我们可以用名称访问对象。目录服务是一种命名服务,在这种服务里,对象不但有名称,还有属性。  “还有属性”是个什么?

 

 例子,访问文件系统:需要把两个jar:fscontext.jar和providerutil.jar加入到你的CLASSPATH中。

[java] view
plain
copy

  1. public class Lookup {  
  2.     public static void main(String[] args) {  
  3.         Hashtable<String, String> env = new Hashtable<String, String>();  
  4.         env.put(Context.INITIAL_CONTEXT_FACTORY,  
  5.                 "com.sun.jndi.fscontext.RefFSContextFactory");  
  6.   
  7.         String name = "F://ecclient";  
  8.         try {  
  9.             // Create the initial context  
  10.             Context ctx = new InitialContext(env);  
  11.   
  12.             // Look up an object  
  13.             Object obj = ctx.lookup(name);  
  14.   
  15.             // Print it  
  16.             System.out.println(name + " is bound to: " + obj + "/t"  
  17.                     + obj.getClass());  
  18.   
  19.         } catch (NamingException e) {  
  20.             System.err.println("Problem looking up " + name + ": " + e);  
  21.         }  
  22.     }  
  23. }  

 

打印结果

[c-sharp] view
plain
copy

  1. F:/ecclient is bound to: com.sun.jndi.fscontext.RefFSContext@901887 class com.sun.jndi.fscontext.RefFSContext  

抱歉!评论已关闭.