Java 无法实例化类:org.jnp.interfaces.NamingContextFactory

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20732328/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:53:13  来源:igfitidea点击:

Cannot instantiate class: org.jnp.interfaces.NamingContextFactory

javamavenejb

提问by nightin_gale

Here this my code:

这是我的代码:

SpeakerRemote.java

扬声器遥控器.java

package test;
import javax.ejb.Remote;

@Remote
public interface SpeakerRemote {
    String sayAPhrase( String phrase );
}


SpeakerBean.java

扬声器Bean.java

package test;
import javax.ejb.Stateless;

@Stateless
public class SpeakerBean implements SpeakerRemote {
    @Override
    public String sayAPhrase( String phrase ){
         return "Speaker Service:\t" + phrase;
    }
}


I assembled it with maven. Here I show you invoking part:

我用maven组装了它。在这里,我向您展示调用部分:

Invoker.java

调用程序

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Properties;

public class Invoker {
    public static InitialContext getContext() throws NamingException {
        final Properties properties = new Properties();
        properties.put( Context.INITIAL_CONTEXT_FACTORY,
            "org.jnp.interfaces.NamingContextFactory" );
        properties.put( Context.PROVIDER_URL, "jnp://127.0.0.1:1099" );
        return new InitialContext( properties );
    }
}


Main.java

主程序

import test.SpeakerRemote;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class Main {
    public static void main( String... args ) {
        try {
            InitialContext context = Invoker.getContext();
            SpeakerRemote speaker = ( SpeakerRemote ) context.lookup( "SpeakerBean/remote" );
            System.out.println( speaker.sayAPhrase( "Hello, World!" ) );
        }
        catch ( NamingException e ) {
            e.printStackTrace();
        }
    }
}

Having started this app I received this exception:

启动这个应用程序后,我收到了这个异常:

"Cannot instantiate class: org.jnp.interfaces.NamingContextFactory [Root exception is java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory]"

“无法实例化类:org.jnp.interfaces.NamingContextFactory [Root 异常是 java.lang.ClassNotFoundException:org.jnp.interfaces.NamingContextFactory]”

Please help me, because I really need to understand it!

请帮助我,因为我真的需要了解它!

P.S. I use jboss 7.1.1 Final + EJB 3.1 + Maven 3.1.1 + Java 1.7 + Win7

PS 我使用 jboss 7.1.1 Final + EJB 3.1 + Maven 3.1.1 + Java 1.7 + Win7

回答by Karthikeyan Sukkoor

Try this code

试试这个代码

package test;
import javax.ejb.Stateless;
import org.jboss.ejb3.annotation.RemoteBinding;  

@Stateless
@RemoteBinding(jndiBinding="SpeakerBean/remote")
public class SpeakerBean implements SpeakerRemote {
@Override
 public String sayAPhrase( String phrase ){
     return "Speaker Service:\t" + phrase;
}
}

回答by Qwertovsky

Read this documentation for JBoss 7.1

阅读 JBoss 7.1 的文档

Remote EJB invocations via JNDI - EJB client API or remote-naming project

通过 JNDI 的远程 EJB 调用 - EJB 客户端 API 或远程命名项目

Add to classpath jboss-client.jar from $JBOSS_HOME/bin/client

从 $JBOSS_HOME/bin/client 添加到类路径 jboss-client.jar

Invoker.java

调用程序

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");
// username
properties.put(Context.SECURITY_PRINCIPAL, "user");
// password
properties.put(Context.SECURITY_CREDENTIALS, "password");
// This is an important property to set if you want to do EJB invocations via the remote-naming project
properties.put("jboss.naming.client.ejb.context", true);
// create a context passing these properties
return new InitialContext(properties);

Main.java

主程序

// lookup the bean    
SpeakerRemote speaker = (SpeakerRemote) context.lookup("myapp/myejbmodule/SpeakerBean!test.SpeakerRemote);
System.out.println( speaker.sayAPhrase( "Hello, World!" ) )

Other approach: EJB invocations from a remote client using JNDI

其他方法:使用 JNDI 从远程客户端调用 EJB

Add to classpath file jboss-ejb-client.properties

添加到类路径文件 jboss-ejb-client.properties

jboss-ejb-client

jboss-ejb-客户端

remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
#if you test on local machine
remote.connection.default.connect.options.org.xnio.Options.SASL_DISALLOWED_MECHANISMS=JBOSS-LOCAL-USER

remote.connections=default
remote.connection.default.host=127.0.0.1
remote.connection.default.port = 4447
remote.connection.default.username=user
remote.connection.default.password=password

Invoker.java

调用程序

final Hashtable<String, String> props = new Hashtable<String, String>();
props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
return new InitialContext(props);

Main.java

主程序

SpeakerRemote speaker = (SpeakerRemote) context.lookup("ejb:myapp/myejbmodule/SpeakerBean!test.SpeakerRemote);
System.out.println( speaker.sayAPhrase( "Hello, World!" ) )