javax.naming.NoInitialContextException:无法使用哈希表中指定的工厂创建 InitialContext
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4178091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javax.naming.NoInitialContextException: Failed to create InitialContext using factory specified in hashtable
提问by Sharad Ahire
I have small program which just creating intial context in unmanaged environment i.e. outside the container.I have been using Websphere 7.0. I have written following program to do the connection with application which is running on WAS 7 using corba url,
我有一个小程序,它只是在非托管环境中创建初始上下文,即容器外。我一直在使用 Websphere 7.0。我编写了以下程序来使用 corba url 与在 WAS 7 上运行的应用程序建立连接,
package snippet;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
public class test {
public static void main(String[] args) {
try {
// create initial context
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.ibm.websphere.naming.WsnInitialContextFactory");
env
.put(Context.PROVIDER_URL,
"corbaloc:iiop:[email protected]:2809/NameService");
InitialContext ctx = new InitialContext(env);
System.out.println(ctx);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
I have created runnable jar and executing using shell script given below,
我创建了可运行的 jar 并使用下面给出的 shell 脚本执行,
#!/bin/sh
WAS_ROOT_PATH=/application/WebSphere/AppServer
SCHEDULER_JAR=/application/apps/JobScheduler/testJNDI.jar
SCHEDULE_FILE=/application/apps/JobScheduler/schedule.xml
. "$WAS_ROOT_PATH"/bin/setupCmdLine.sh
CLASSPATH="$MQLIB":"$WAS_CLASSPATH"
"$JAVA_HOME"/bin/java -classpath "$CLASSPATH" -jar "$SCHEDULER_JAR"
Ater running i have been facing below exception,
在跑步之后,我一直面临以下异常,
$ testJNDI.sh
javax.naming.NoInitialContextException: Failed to create InitialContext using factory specified in hashtable {java.naming.provider.url=corbaloc:iiop:[email protected]:2809/NameService, java.naming.factory.initial=com.ibm.websphere.naming.WsnInitialContextFactory} [Root exception is java.lang.NullPointerException]
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:243)
at javax.naming.InitialContext.initializeDefaultInitCtx(InitialContext.java:327)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:357)
at javax.naming.InitialContext.internalInit(InitialContext.java:295)
at javax.naming.InitialContext.<init>(InitialContext.java:212)
at snippet.test.main(test.java:19)
Caused by: java.lang.NullPointerException
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:235)
... 5 more
$
I got stuck on above problem but i could not get why this is happening.
我被上述问题困住了,但我不明白为什么会这样。
Please do the needful to get out me from above problem.
请做必要的事情让我摆脱上述问题。
回答by Pascal Thivent
Are you sure the provider URL is correct? According to various examples like this one(WAS Express 6) or this one(WAS 8) (couldn't find a reference for WAS 7 but it looks like things didn't change), the code to use a CORBA object URL with the WAS JNDI implementation is described as follows:
您确定提供者 URL 正确吗?根据这个(WAS Express 6)或这个(WAS 8)(找不到WAS 7的参考,但看起来事情没有改变)这样的各种例子,使用CORBA对象URL的代码WAS JNDI 实现描述如下:
Using a CORBA object URL
This example shows a CORBA object URL.
... import java.util.Hashtable; import javax.naming.Context; import javax.naming.InitialContext; ... Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory"); env.put(Context.PROVIDER_URL, "corbaloc:iiop:myhost.mycompany.com:2809"); Context initialContext = new InitialContext(env); ...
使用 CORBA 对象 URL
此示例显示了一个 CORBA 对象 URL。
... import java.util.Hashtable; import javax.naming.Context; import javax.naming.InitialContext; ... Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory"); env.put(Context.PROVIDER_URL, "corbaloc:iiop:myhost.mycompany.com:2809"); Context initialContext = new InitialContext(env); ...
Don't know if this will help though.
不知道这是否会有所帮助。
回答by Manglu
The Provider URL helps you to identify the server and the root @ which you connect to the name space.
提供程序 URL 可帮助您识别连接到名称空间的服务器和根@。
As an example, if you wanted to connect to the Cell Persisent root, you would specify the the Provider_URL as:
例如,如果您想连接到 Cell Persisent 根,您可以将 Provider_URL 指定为:
env.put(Context.PROVIDER_URL, "corbaloc:iiop:myhost.mycompany.com:2809/NameServiceCellPersistentRoot");
env.put(Context.PROVIDER_URL, "corbaloc:iiop:myhost.mycompany.com:2809/NameServiceCellPersistentRoot");
Server Root NameServiceServerRoot Cell Persistent Root NameServiceCellPersistentRoot Cell Root NameServiceCellRoot Node Root NameServiceNodeRoot
服务器根名称ServiceServerRoot Cell Persistent Root NameServiceCellPersistentRoot Cell Root NameServiceCellRoot Node Root NameServiceNodeRoot
The default object key is:"NameService" so stating that in the URL is not required if you want to connect to the default location.
默认对象键是:“ NameService”,因此如果您想连接到默认位置,则不需要在 URL 中进行说明。
Also, is there anything else in the stack trace that states any other information?
此外,堆栈跟踪中是否还有其他任何内容可以说明任何其他信息?
Also from the client machine is the DNS name: x1devapp63.dev.freightliner.comresolvable?
同样来自客户端机器的是 DNS 名称:x1devapp63.dev.freightliner.com可解析吗?
Is this the name that is used by the WAS Server to identify itself? When WAS is installed you do specify a host name and does that match this name?
这是 WAS 服务器用来标识自己的名称吗?安装 WAS 时,您确实指定了一个主机名,并且该名称与该名称匹配吗?
Can you also run the same from the same machine as the WAS Server and use localhostand see if the errors are the same.
您是否也可以从与 WAS 服务器相同的机器上运行相同的机器并使用localhost并查看错误是否相同。
I am just thinking about potential network related errors which are probably causing trouble.
我只是在考虑可能导致麻烦的潜在网络相关错误。
Have a look at this for the various values that help you could connect to for the root context.
看看这个可以帮助您连接到根上下文的各种值。
HTH Manglu
HTH芒陆
回答by unR
Don't forget to include the thinclient-jars to your buildpath. You will need them to do a jndi lookup from a standalone client.
不要忘记将 Thinclient-jar 包含到您的构建路径中。您将需要它们从独立客户端进行 jndi 查找。
%WAS_HOME%/runtimes is where they can be found.
%WAS_HOME%/runtimes 是可以找到它们的地方。