java 连接 websphere_本地java类访问websphere的JNDI

本文介绍了在Java环境中连接Websphere应用服务器的JNDI资源时遇到的问题及解决方案,包括NoInitialContextException错误、配置InitialContext、以及Junit调试时的数据源连接问题。关键在于正确设置JNDI环境参数,如INITIAL_CONTEXT_FACTORY、PROVIDER_URL等,并确保classpath中包含正确的Websphere客户端jar文件。
摘要由CSDN通过智能技术生成

# 问题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 env= new Hashtable();                  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 5Hashtable 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.RefFSContextFactoryLDAPv3:    Com.sun.jndi.ldap.LdapCtxFactoryNDS:     com.novell.naming.service.nds.NdsInitialContextFactoryNIS:     com.sun.jndi.nis.NISCtxFactoryRMI registry: com.sun.jndi.rmi.registry.RegistryContextFactoryIBM LDAP服务提供者:   com.ibm.jndi.LDAPCtxFactoryBEA 名字服务提供者:   weblogic.jndi.WLInitialContextFactoryJBOSS名字服务提供者:  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.BlockJUnit4Cla***unner.runNotIgnored(BlockJUnit4Cla***unner.java:79)         at org.junit.runners.BlockJUnit4Cla***unner.runChild(BlockJUnit4Cla***unner.java:71)         at org.junit.runners.BlockJUnit4Cla***unner.runChild(BlockJUnit4Cla***unner.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.BlockJUnit4Cla***unner.runNotIgnored(BlockJUnit4Cla***unner.java:79)         at org.junit.runners.BlockJUnit4Cla***unner.runChild(BlockJUnit4Cla***unner.java:71)         at org.junit.runners.BlockJUnit4Cla***unner.runChild(BlockJUnit4Cla***unner.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.(EJSClientORBImpl.java:83)         at com.ibm.ejs.oa.EJSClientORBImpl.(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.BlockJUnit4Cla***unner.runNotIgnored(BlockJUnit4Cla***unner.java:79)         at org.junit.runners.BlockJUnit4Cla***unner.runChild(BlockJUnit4Cla***unner.java:71)         at org.junit.runners.BlockJUnit4Cla***unner.runChild(BlockJUnit4Cla***unner.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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值