gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:49:51 24 4
gpt4 key购买 nike

这是我的代码:

SpeakerRemote.java

package test;
import javax.ejb.Remote;

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

SpeakerBean.java

package test;
import javax.ejb.Stateless;

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

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

调用者.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 );
}
}

主.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();
}
}
}

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

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

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

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

最佳答案

阅读 JBoss 7.1 的文档

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

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

调用者.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);

主程序.java

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

其他方法:EJB invocations from a remote client using JNDI

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

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

调用者.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);

主程序.java

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

关于java - 无法实例化类 : org. jnp.interfaces.NamingContextFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20732328/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com