Java JNDI“无法实例化类:org.jboss.naming.remote.client.InitialContextFactory”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21213693/
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
JNDI "Cannot instantiate class: org.jboss.naming.remote.client.InitialContextFactory"
提问by user3188370
I'm using JBoss Server for EJB And I need JNDI in console app to get reference of session bean, console app code looks like this
我正在将 JBoss 服务器用于 EJB 我需要在控制台应用程序中使用 JNDI 来获取会话 bean 的引用,控制台应用程序代码如下所示
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class Program {
public static void main(String[] args) throws NamingException {
// TODO Auto-generated method stub
Properties pr = new Properties();
pr.put(InitialContext.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
pr.put(InitialContext.PROVIDER_URL,"remote://localhost:4447");
InitialContext ic = new InitialContext(pr);
}
}
when I run the application I get exception
当我运行应用程序时出现异常
Exception in thread "main" javax.naming.NoInitialContextException: Cannot instantiate class: org.jboss.naming.remote.client.InitialContextFactory [Root exception is java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory]
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 Program.main(Program.java:14)
Caused by: java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory
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)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.sun.naming.internal.VersionHelper12.loadClass(Unknown Source)
... 5 more
采纳答案by Vinayak Pingale
You can use following context to connect. I have tried and tested to set up this.
您可以使用以下上下文进行连接。我已经尝试并测试来设置它。
import java.util.Properties;
import javax.naming.Context;
import javax.naming.NamingException;
public class Program {
public static void main(String[] args) throws NamingException {
Properties jndiProps = new Properties();
jndiProps.put(Context.INITIAL_CONTEXT_FACTORY,
"org.jboss.naming.remote.client.InitialContextFactory");
jndiProps.put(Context.PROVIDER_URL,"remote://localhost:4447");
jndiProps.put(Context.SECURITY_PRINCIPAL, "testuser");
jndiProps.put(Context.SECURITY_CREDENTIALS, "testpassword");
jndiProps.put("jboss.naming.client.ejb.context", true);
Context ctx = new InitialContext(jndiProps);
}
}
Then i got this error
然后我得到了这个错误
JBREM000200: Remote connection failed: javax.security.sasl.SaslException:
Authentication failed: all available authentication mechanisms failed - Could
not register a EJB receiver for connection to remote://localhost:4447
java.lang.RuntimeException: javax.security.sasl.SaslException: Authentication
failed: all available authentication mechanisms failed.
Then i added the user using add-user.sh.
然后我使用 add-user.sh 添加了用户。
Successful Handshakemessage came.
成功握手消息来了。
回答by gredezcici
you may need add jboss-client.jar and jboss-ejb3-common-client.jar into your library
您可能需要将 jboss-client.jar 和 jboss-ejb3-common-client.jar 添加到您的库中
回答by Jagdish Chandra
To overcome above error you need to pay attention to two points.
First , You need to have jboss-client.jarin classpath.
Second, according to Jboss version you are using you need to change your provider url.
要克服上述错误,您需要注意两点。
首先,您需要在类路径中有jboss-client.jar。
其次,根据您使用的 Jboss 版本,您需要更改提供程序 url。
For JBossAS 5you should set following properties in environment
对于JBossAS 5,您应该在环境中设置以下属性
env.put(Context.INITIAL_CONTEXT_FACTORY,"org.jboss.naming.NamingContextFactory");
env.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
env.put(Context.PROVIDER_URL, "jnp://127.0.0.1:1099");
For JBossAS 7you should set following properties in environment
对于JBossAS 7,您应该在环境中设置以下属性
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
env.put(Context.PROVIDER_URL, System.getProperty(Context.PROVIDER_URL, "remote://localhost:4447"));
env.put(Context.SECURITY_PRINCIPAL, System.getProperty("username", DEFAULT_USERNAME));
env.put(Context.SECURITY_CREDENTIALS, System.getProperty("password", DEFAULT_PASSWORD));
回答by Mirimas
You need dependency:
你需要依赖:
<dependency>
<groupId>org.jboss</groupId>
<artifactId>jboss-remote-naming</artifactId>
</dependency>
https://mvnrepository.com/artifact/org.jboss/jboss-remote-naming/
https://mvnrepository.com/artifact/org.jboss/jboss-remote-naming/
For WildFly you can use org.wildfly.naming.client.WildFlyInitialContextFactory form:
对于 WildFly,您可以使用 org.wildfly.naming.client.WildFlyInitialContextFactory 形式:
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-naming-client</artifactId>
</dependency>
https://mvnrepository.com/artifact/org.wildfly/wildfly-naming-client
https://mvnrepository.com/artifact/org.wildfly/wildfly-naming-client
You can use bom file for import:
您可以使用 bom 文件进行导入:
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-ejb-client-bom</artifactId>
<type>pom</type>
</dependency>
https://mvnrepository.com/artifact/org.wildfly/wildfly-ejb-client-bom
https://mvnrepository.com/artifact/org.wildfly/wildfly-ejb-client-bom