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

时间:2015-01-26 15:04:25

标签: class jboss jboss7.x

我正在尝试将我的客户端应用程序连接到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

我做错了什么?

1 个答案:

答案 0 :(得分:0)

FYI, 如果您使用 Wildfly JMS ,请将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>