我正在使用JBoss Server for 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);
}
}
当我运行应用程序时,我得到异常
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$1.run(Unknown Source)
at java.net.URLClassLoader$1.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
答案 0 :(得分:10)
要克服上述错误,您需要注意两点
首先,您需要在 classpath 中使用 jboss-client.jar 。
其次,根据您使用的Jboss版本,您需要更改提供商网址。
对于 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");
对于 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));
答案 1 :(得分:2)
您可以使用以下上下文进行连接。 我已经尝试并测试过这个设置。
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);
}
}
然后我收到了这个错误
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.
然后我使用add-user.sh添加了用户。
成功握手消息传来。
答案 2 :(得分:2)
您可能需要将jboss-client.jar和jboss-ejb3-common-client.jar添加到您的库中
答案 3 :(得分:0)
您需要依赖项:
<dependency>
<groupId>org.jboss</groupId>
<artifactId>jboss-remote-naming</artifactId>
</dependency>
https://mvnrepository.com/artifact/org.jboss/jboss-remote-naming/
对于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
您可以使用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