java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory 错误

Posted

技术标签:

【中文标题】java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory 错误【英文标题】:java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory error 【发布时间】:2015-03-25 00:30:58 【问题描述】:

我正在尝试将我的客户端应用程序连接到 JBoss 7.1.1。 MHSessionBeanRemote 是我的服务器端 bean 类。

我的班级代码

import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class MHSessionBeanClient 
    public static void main(String[] args) throws NamingException 
        Context context = MHSessionBeanClient.getInitialContext();
        MHSessionBeanRemote mhSessionBean = (MHSessionBeanRemote)context.lookup("MHSessionBean/remote");
        mhSessionBean.mhSessionBeanMethod();
       
    public static Context getInitialContext() throws NamingException 
        Properties properties = new Properties();
        properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
        properties.put(Context.PROVIDER_URL,"remote://127.0.0.1:4447");
        properties.put(Context.SECURITY_PRINCIPAL, "user");
        properties.put(Context.SECURITY_CREDENTIALS, "password");
        properties.put("jboss.naming.client.ejb.context", true);
        return new InitialContext(properties);
    

和错误:

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 MHSessionBeanClient.getInitialContext(MHSessionBeanClient.java:25)
at MHSessionBeanClient.main(MHSessionBeanClient.java:12)
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)
at com.sun.naming.internal.VersionHelper12.loadClass(Unknown Source)
... 6 more

我做错了什么?

【问题讨论】:

检查你是否按照***.com/questions/21213693/…的帖子在类路径中添加了jboss-client.jar @Rao 谢谢先生。这解决了问题。 【参考方案1】:

仅供参考, 如果您使用 WildflyJMS,请将 wildfly-jms-client-bom 添加到您的 pom 中;

    <dependency>
        <groupId>org.wildfly</groupId>
        <artifactId>wildfly-jms-client-bom</artifactId>
        <version>10.0.0.Final</version>
        <type>pom</type>
    </dependency>

【讨论】:

以上是关于java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory 错误的主要内容,如果未能解决你的问题,请参考以下文章