本地java类访问websphere的JNDI

# 问题1、获取数据源时报错
# javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet 
# 代码:dataSource=(DataSource)envCtx.lookup("jdbc/admin"); 
# 原因:本地测试如果要访问远程的JNDI资源,就必须用饱含JNDI环境参数Hashtable初始化InitialContext。
# 必要的环境参数如:
# Context.INITIAL_CONTEXT_FACTORY//连接工厂
# Context.PROVIDER_URL//访问连接
# Context.SECURITY_PRINCIPAL//安全用户
# Context.SECURITY_CREDENTIALS//用户密码

问题2、配置InitialContext

# websphere 7
 Hashtable<String,String> env= new Hashtable<String,String>();                 
 env.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory"); 
 env.put(Context.PROVIDER_URL, "iiop://localhost:2809"); 
 ctx = new InitialContext(env); 
 dataSource=(DataSource)context.lookup("jdbc/admin");

# websphere 5
Hashtable parms = new Hashtable();
      parms.put(Context.INITIAL_CONTEXT_FACTORY, 
                com.ibm.websphere.naming.WsnInitialContextFactory.class.getName());
      parms.put(Context.PROVIDER_URL, "iiop://localhost:900/");
      Context ic = new InitialContext();
      DataSource ds = (DataSource) ic.lookup("userdb");

//jboss:
Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory"
Context.URL_PKG_PREFIXES, "org.jboss.naming"
Context.PROVIDER_URL, "localhost:1099"
//weblogic:
Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"
Context.PROVIDER_URL, "t3://localhost:7001"
//apusic(金蝶):
Context.INITIAL_CONTEXT_FACTORY, "com.apusic.jndi.InitialContextFactory"
Context.PROVIDER_URL, "rmi://localhost:6888"
//WebSphere 5:
Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory"
Context.PROVIDER_URL, "iiop://localhost:900"

//J2EE  SDK(J2EE  RI):
Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory"
Context.PROVIDER_URL, "iiop://127.0.0.1:1050"
//SilverStream:
Context.INITIAL_CONTEXT_FACTORY, "com.sssw.rt.jndi.AgInitCtxFactory"
Context.PROVIDER_URL, "sssw://localhost:80"
//OC4J:
Context.INITIAL_CONTEXT_FACTORY, "com.evermind.server.rmi.RMIInitialContextFactory"
Context.PROVIDER_URL, "ormi://127.0.0.1/"
//WAS5:
Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory"
Context.PROVIDER_URL, "iiop://localhost:2809"

常用JNDI服务提供者连接工厂:
Filesystem:  Com.sun.jndi.fscontext.FSContextFactory或者com.sun.jndi.fscontext.RefFSContextFactory
LDAPv3:    Com.sun.jndi.ldap.LdapCtxFactory
NDS:     com.novell.naming.service.nds.NdsInitialContextFactory
NIS:     com.sun.jndi.nis.NISCtxFactory
RMI registry: com.sun.jndi.rmi.registry.RegistryContextFactory
IBM LDAP服务提供者:   com.ibm.jndi.LDAPCtxFactory
BEA 名字服务提供者:   weblogic.jndi.WLInitialContextFactory
JBOSS名字服务提供者:  org.jnp.interfaces.NamingContextFactory
# 问题3、数据源配置成功后,Junit调试报错
# java.lang.NoClassDefFoundError:com/ibm/ws/bootstrp/raswsloggerFactory
# 原因:com.ibm.websphere.naming.WsnInitialContextFactory这个类找不到,需要把websphere的客户端使用的.jar包含在classpath中。
com.ibm.ws.ejb.thinclient_7.0.0.jar  
# javax.naming.NamingException: Failed to initialize the ORB [Root exception is org.omg.CORBA.INITIALIZE: can't instantiate default ORB implementation com.ibm.CORBA.iiop.ORB  vmcid: 0x0  minor code: 0  completed: No] 
com.ibm.ws.orb_7.0.0.jar 
#所需websphere环境中的jar包
com.ibm.ws.ejb.thinclient_7.0.0.jar  
com.ibm.ws.orb_7.0.0.jar 
(下面测试中未使用)
com.ibm.ws.runtime.jar  
ibmorb.jar   
总结:
我开始测试时只是在本地建了一个JAVA类并没有部署一个应用程序到websphere中。在websphere中部署一个应该程序后已经可以获得连接了。
不在websphere中部署应用程序而想测试连接是否成功,则需要在getConnection()方法中传入数据库用户名与密码。
如:
Hashtable parms = new Hashtable();
      parms.put(Context.INITIAL_CONTEXT_FACTORY, 
                com.ibm.websphere.naming.WsnInitialContextFactory.class.getName());
      parms.put(Context.PROVIDER_URL, "iiop://localhost:900/");
      Context ic = new InitialContext();
      ds = (DataSource) ic.lookup("jndi/dsc");                
      Connection conn = ds.getConnection("userdb","userdb");

com.ibm.ws.admin.client_7.0.0.jar 
java.lang.NoClassDefFoundError: org/eclipse/wst/common/internal/emf/utilities/EncoderDecoder 
        at java.lang.ClassLoader.defineClass1(Native Method) 
        at java.lang.ClassLoader.defineClass(ClassLoader.java:800) 
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) 
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:449) 
        at java.net.URLClassLoader.access$100(URLClassLoader.java:71) 
        at java.net.URLClassLoader$1.run(URLClassLoader.java:361) 
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355) 
        at java.security.AccessController.doPrivileged(Native Method) 
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354) 
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425) 
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) 
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358) 
        at com.ibm.ws.naming.util.WsnInitCtxFactory.decodeCredentials(WsnInitCtxFactory.java:778) 
        at com.ibm.ws.naming.util.WsnInitCtxFactory.getInitialContextInternal(WsnInitCtxFactory.java:428) 
        at com.ibm.ws.naming.util.WsnInitCtx.getContext(WsnInitCtx.java:123) 
        at com.ibm.ws.naming.util.WsnInitCtx.getContextIfNull(WsnInitCtx.java:798) 
        at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:164) 
        at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:179) 
        at javax.naming.InitialContext.lookup(InitialContext.java:411) 
        at cn.com.icbc.netm.utils.DataSourceUtils.getDataSource(DataSourceUtils.java:52) 
        at cn.com.icbc.netm.utils.DataSourceUtils.getConnection(DataSourceUtils.java:69) 
        at cn.com.icbc.netm.demo.junitDemo.show(junitDemo.java:31) 
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
        at java.lang.reflect.Method.invoke(Method.java:606) 
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
        at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) 
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) 
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) 
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
        at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 
Caused by: java.lang.ClassNotFoundException: org.eclipse.wst.common.internal.emf.utilities.EncoderDecoder 
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366) 
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355) 
        at java.security.AccessController.doPrivileged(Native Method) 
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354) 
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425) 
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) 
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358) 
        ... 45 more

com.ibm.ws.ejb.thinclient_7.0.0.jar

获取datasource失败 
javax.naming.NamingException: Failed to initialize the ORB [Root exception is java.lang.ClassCastException: com.sun.corba.se.impl.orb.ORBImpl cannot be cast to com.ibm.CORBA.iiop.ORB] 
        at com.ibm.ws.naming.util.Helpers.getOrb(Helpers.java:364) 
        at com.ibm.ws.naming.util.WsnInitCtxFactory.getInitialContextInternal(WsnInitCtxFactory.java:421) 
        at com.ibm.ws.naming.util.WsnInitCtx.getContext(WsnInitCtx.java:123) 
        at com.ibm.ws.naming.util.WsnInitCtx.getContextIfNull(WsnInitCtx.java:798) 
        at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:164) 
        at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:179) 
        at javax.naming.InitialContext.lookup(InitialContext.java:411) 
        at cn.com.icbc.netm.utils.DataSourceUtils.getDataSource(DataSourceUtils.java:52) 
        at cn.com.icbc.netm.utils.DataSourceUtils.getConnection(DataSourceUtils.java:69) 
        at cn.com.icbc.netm.demo.junitDemo.show(junitDemo.java:31) 
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
        at java.lang.reflect.Method.invoke(Method.java:606) 
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
        at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) 
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) 
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) 
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
        at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 
Caused by: java.lang.ClassCastException: com.sun.corba.se.impl.orb.ORBImpl cannot be cast to com.ibm.CORBA.iiop.ORB 
        at com.ibm.ws.orb.GlobalORBFactory.init(GlobalORBFactory.java:92) 
        at com.ibm.ejs.oa.EJSORBImpl.initializeORB(EJSORBImpl.java:179) 
        at com.ibm.ejs.oa.EJSClientORBImpl.<init>(EJSClientORBImpl.java:83) 
        at com.ibm.ejs.oa.EJSClientORBImpl.<init>(EJSClientORBImpl.java:59) 
        at com.ibm.ejs.oa.EJSORB.init(EJSORB.java:102) 
        at com.ibm.ws.naming.util.Helpers.getOrb(Helpers.java:356) 
        ... 32 more 
获取数据失败 
java.lang.NullPointerException 
        at cn.com.icbc.netm.utils.DataSourceUtils.getConnection(DataSourceUtils.java:69) 
        at cn.com.icbc.netm.demo.junitDemo.show(junitDemo.java:31) 
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
        at java.lang.reflect.Method.invoke(Method.java:606) 
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
        at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) 
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) 
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) 
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
        at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

原文地址:http://blog.51cto.com/f1yinsky/2103803

时间: 2024-08-30 07:16:24

本地java类访问websphere的JNDI的相关文章

java 类访问权限

Java有四种访问权限, 其中三种有访问权限修饰符,分别为private,public和protected,还有一种不带任何修饰符. private: Java语言中对访问权限限制的最窄的修饰符,一般称之为“私有的”.被其修饰的类.属性以及方法只能被该类的对象访问,其子类不能访问,更不能允许跨包访问. default:即不加任何访问修饰符,通常称为“默认访问模式“.该模式下,只允许在同一个包中进行访问. protect: 介于public 和 private 之间的一种访问修饰符,一般称之为“保

JAVA类访问URL

URI uri = new URI("https://www.baidu.com/");        Desktop desktop = null;        if (Desktop.isDesktopSupported()) {         desktop = Desktop.getDesktop();        }        if (desktop != null)         desktop.browse(uri);       } catch (IOExc

java类的访问权限

1.解析 Java有四种访问权限, 其中三种有访问权限修饰符,分别为private,public和protected,还有一种不带任何修饰符. private: Java语言中对访问权限限制的最窄的修饰符,一般称之为“私有的”.被其修饰的类.属性以及方法只能被该类的对象访问,其子类不能访问,更不能允许跨包访问. default:即不加任何访问修饰符,通常称为“默认访问模式“.该模式下,只允许在同一个包中进行访问. protect: 介于public 和 private 之间的一种访问修饰符,一般

Java知多少(111)几个重要的java数据库访问类和接口

修改数据表记录也有3种方案. 一.使用Statement对象 实现修改数据表记录的SQL语句的语法是:    update表名 set 字段名1 = 字段值1,字段名2 = 字段值2,……where特定条件例如: update ksInfo set 姓名 = ‘张小卫’where 姓名 = ‘张大卫’ 先创建一个SQL语句,然砶调用Statement对象的executeUpdate()方法.例如, 1 sql = “update ksInfo set 姓名 = ‘”+txtName.getTex

java类中修饰符public,protected等访问权限总结

public   protected  ……   private       类    +          +         +       +       包    +          +         +      继承   +         +      其他   + java类中修饰符public,protected等访问权限总结

如果在本地搭一个服务器和mysql数据库环境,如果使用java来访问数据库

我们可以使用speedamp来搭一个服务器环境,可以在http://download.csdn.net/detail/baidu_nod/7630265下载 解压后无需安装直接可以使用,点击SpeedAMP.exe,然后可以通过http://localhost/phpmyadmin/index.php来访问本地数据库 java可以通过jdbc来访问数据库,比较重要的是下载一个mysql-connector-java-5.1.22-bin.jar文件,然后在java工程中引用, 在代码里可以这样来

Error:(37, 47) java: 无法访问org.apache.hadoop.mapred.JobConf 找不到org.apache.hadoop.mapred.JobConf的类文件

Error:(37, 47) java: 无法访问org.apache.hadoop.mapred.JobConf   找不到org.apache.hadoop.mapred.JobConf的类文件 出现此异常,就是缺少相关的依赖包,你检查一下以下四个依赖包是否齐全: hadoop-mapreduce-client-core-2.7.2.jar hadoop-common-2.7.2.jar hadoop-mapreduce-client-common-2.7.2.jar hadoop-mapr

java中使用URLClassLoader访问外部jar包的java类

很多时候 我们写的Java程序是分模块的,有很好的扩展机制,即我们可以为我们自己的java类添加插件,来运行将来某天我们可能开发出来的类,以下称这些类为插件类. 下边是一种简单的实现方法: Class A 作为程序的主入口,其中包含了程序的执行入口(main)函数.然后在main函数中通过外部的配置文件,然后通过外部的配置文件,我们可以获得插件类的信息(位于哪个jar包,jar包的具体路径),然后获得jar包中某一个类的实例,来完成相应的工作.这个jar包很可能是外部的jar包,是我们自己写好的

Java类WebServer及中间件拿webshell方法总结

0.序 原文名称:Tomcat.Weblogic.JBoss.GlassFish.Resin.Websphere弱口令及拿webshell方法总结 原文from:http://www.hack80.com/thread-22662-1-1.html 1.java应用服务器 Java应用服务器主要为应用程序提供运行环境,为组件提供服务.Java 的应用服务器很多,从功能上分为两类:JSP 服务器和 Java EE 服务器. 1.1 常见Java服务器 常见的Java服务器:Tomcat.Weblo