Java 无法使用 JBoss 服务器实例化 InitialContext
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20784480/
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
Cannot instantiate InitialContext with JBoss server
提问by javaLovah
I'm trying to create a InitialContext
so I could ask the JNDI for some enterprise java beans.
The JBoss is running fine, but when I run the java code I get an exception.
我正在尝试创建一个,InitialContext
以便我可以向 JNDI 询问一些企业 Java bean。JBoss 运行良好,但是当我运行 java 代码时出现异常。
I'm running JBoss 7.1
我正在运行 JBoss 7.1
Here is my code:
这是我的代码:
public class Test {
public static void main(String[] args){
InitialContext ctx=getInitialContext();
Object ref=null;
try {
ref = ctx.lookup("ParamEJB/remote");
} catch (NamingException e) {
System.out.println("Lookup Failed");
e.printStackTrace();
}
Param stub=(Param)PortableRemoteObject.narrow(ref, Param.class);
int times=stub.getTimes();
for(int i=0;i<times;i++)
System.out.println(stub.getMessage());
}
public static InitialContext getInitialContext(){
Hashtable<String,String> h=new Hashtable<String,String>();
h.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
h.put("java.naming.provider.url","localhost");
try {
return new InitialContext(h);
} catch (NamingException e) {
System.out.println("Cannot generate InitialContext");
e.printStackTrace();
}
return null;
}
}
And after i start my JBoss server, I try to run the java code and I get this exception:
在我启动 JBoss 服务器后,我尝试运行 java 代码,但出现此异常:
javax.naming.NoInitialContextException: Cannot instantiate class: org.jnp.interfaces.NamingContextFactory [Root exception is java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory]
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.init(Unknown Source)
at javax.naming.InitialContext.<init>(Unknown Source)
at client.Test.getInitialContext(Test.java:32)
at client.Test.main(Test.java:13)
Caused by: java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory
at java.net.URLClassLoader.run(Unknown Source)
at java.net.URLClassLoader.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
What might be the problem?
可能是什么问题?
采纳答案by Gabriel Aramburu
The InitialContext properties are not right for the JBoss version that you are using. With JBoss 7, things have changed considerably when you call a ejb from a remote client. This linkcan help you to instantiate correctly the InitialContex object and to determine the JNDI entry name. Also will tell you what are the necessary dependencies that need to be added to the client classpath.
InitialContext 属性不适合您使用的 JBoss 版本。使用 JBoss 7,当您从远程客户端调用 ejb 时,情况发生了很大变化。此链接可帮助您正确实例化 InitialContex 对象并确定 JNDI 条目名称。还会告诉您需要添加到客户端类路径的必要依赖项是什么。
回答by Thato Pebane
I had the same problem but I found how to fix it. All you have to do is add the jbossall-client.jar library to the clients project, and done!!! You can find the file inside the client folder. e.g jboss-6.1.0.Final_GPT\client I was using Jboss 6.1.0 You can also get help from this link https://community.oracle.com/thread/1157701?start=0
我遇到了同样的问题,但我找到了解决方法。您所要做的就是将 jbossall-client.jar 库添加到客户端项目中,大功告成!!!您可以在客户端文件夹中找到该文件。例如 jboss-6.1.0.Final_GPT\client 我使用的是 Jboss 6.1.0 您也可以从此链接获得帮助https://community.oracle.com/thread/1157701?start=0
Hope it helps.
希望能帮助到你。