Java 无法实例化类:org.jnp.interfaces.NamingContextFactory
这是我的代码: SpeakerRemote.javaJava 无法实例化类:org.jnp.interfaces.NamingContextFactory,java,maven,ejb,Java,Maven,Ejb,这是我的代码: 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 im
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一起组装的。在这里,我向您展示了调用部分: 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();
}
}
}
启动此应用程序后,我收到此异常:
“无法实例化类:org.jnp.interfaces.NamingContextFactory[根异常为java.lang.ClassNotFoundException:org.jnp.interfaces.NamingContextFactory]”
请帮助我,因为我真的需要理解它
另外,我使用jboss 7.1.1 Final+EJB3.1+Maven 3.1.1+Java1.7+Win7,试试下面的代码
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;
}
}
阅读JBoss7.1的文档 从$jboss_HOME/bin/client添加到类路径jboss-client.jar Invoker.java 主类 其他办法: 添加到类路径文件jboss-ejb-client.properties JBossEJB客户端 Invoker.java
不,告诉我怎样才能观察到我不明白你在说什么。我的机器上有Jboss。我可以访问任何目录。告诉我,我该怎么做)我想,异常消息相当整洁。“根异常是java.lang.ClassNotFoundException:org.jnp.interfaces.NamingContextFactory”。您确定在类路径中有所需的jar吗?首先找出类来自哪个jar,然后确保在运行应用程序时,jar在类路径中。此外,您发布的所有代码甚至都不相关。你需要检查你的类路径设置。让我描述一下我做了什么。我使用maven组装将SpeakerRemote.java和SpeakerBean.java放入jar文件,并将其部署在Jboss上。然后我制作了客户端应用程序(Main.java+Invoke.java),并将jar文件添加到lib-dir中。是的,我正在尝试,但仍然没有成功。我在我的libs中添加了:jboss-ejb3-ext-api:1.1.1.jar,并在pom.xml中添加了依赖项。编译后我得到:“未能找到org.jboss.ejb3:jboss-ejb3-build:pom:1.0使用行号[错误]ping我上一个异常未能在项目SpeakerService上执行目标:无法解析项目测试的依赖项:SpeakerService:jar:1.0-SNAPSHOT:未能在org.jboss.ejb3:jboss-ejb3-ext-api:jar:1.1.1->org.jboss.metadata:jboss元数据:jar:1.0.0.CR16:未能读取项目的工件描述符org.jboss.metadata:jboss metadata:jar:1.0.0.CR16:无法将工件org.jboss.metadata:jboss metadata:pom:1.0.0.CR16从/传输到jboss(),但我正在尝试下载另一个ArtefactId我正在尝试使用此()原型,但没有任何本地/远程绑定。类:(我是做什么的?我已经做了你的第一个方法中描述的。但它仍然不起作用。我猜是为什么。我查看了jboss-client.jar,没有在其中找到“org.jboss.naming.remote.client.InitialContextFactory”。我只找到了“org.jboss.naming”。(NamingContextFactory | BridgeNamingContextFactory | HttpNamingContextFactory |和其他人)我的文件中没有您的类。如果您使用maven,请尝试添加dependensy org.jboss.as.jboss-as-ejb-client-bom。在这个示例中查找。在您给我这个示例之前,我已经看完了这个示例。它被描述为jboss.docs。但是我想,您没有理解我所说的。看看“RemoteCalculator”类“LookupRemoteTestTelesCalculator”方法。使用了以下构造:jndProperties.put(Context.URL\u PKG\u前缀,“org.jboss.ejb.client.naming”)请告诉我“org.jboss.ejb.client.naming”位于何处?JVM(或smth其他)位于何处从jboss-client.jar中获取它?我怀疑,它是从jboss-client.jar中获取的。如果它是真的,我会看到。但是通过这个目录,我在jboss-client.jar中找不到它。你看到了吗?是的,我在jboss-client.jar中看到了org.jboss.naming.remote.client.InitialContextFactory。好的,我也找到了。但是听着,告诉我,我如何在我的程序中使用RemoteCalculator?Shell I injec什么?有描述的EJB和EJB客户端。它们被组装成pom文件。我理解。但我如何执行pom项目?我可能只部署它。
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);
// lookup the bean
SpeakerRemote speaker = (SpeakerRemote) context.lookup("myapp/myejbmodule/SpeakerBean!test.SpeakerRemote);
System.out.println( speaker.sayAPhrase( "Hello, World!" ) )
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
final Hashtable<String, String> props = new Hashtable<String, String>();
props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
return new InitialContext(props);
SpeakerRemote speaker = (SpeakerRemote) context.lookup("ejb:myapp/myejbmodule/SpeakerBean!test.SpeakerRemote);
System.out.println( speaker.sayAPhrase( "Hello, World!" ) )