2 sinat 21420571 sinat_21420571 于 2016.04.17 10:21 提问

eclipse+wildfly10创建简单Ejb实例出错

图片说明

package ejb3;//HelloWorld接口

public interface HelloWorld {

public String SayHello(String name);

}

package ejb3.impl;//HelloWorldBean实现接口HelloWorld

import ejb3.HelloWorld;

import javax.ejb.*;

@Stateless

@Remote({HelloWorld.class})

public class HelloWorldBean implements HelloWorld{

public String SayHello(String name){

return name+"这是我的第一个EJB3.";

}

}

package debug;//创建EJBFactory类,用于使用JNDI获取EJB对象

import java.util.*;

import javax.naming.*;;

public class EJBFactory {

public static Object getEJB(String jndipath){

try{

Properties props=new Properties();

props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");

props.setProperty("java.naming.provider.url","localhost:1099");

props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");

InitialContext ctx=new InitialContext(props);

return ctx.lookup(jndipath);

}catch(NamingException e){

e.printStackTrace();

}

return null;

}

package debug;//JUnit测试类

import static org.junit.Assert.*;

import org.junit.BeforeClass;

import org.junit.Test;

import ejb3.HelloWorld;

public class HelloWorldTest {

protected static HelloWorld helloworld;

@BeforeClass

public static void setUpBeforeClass()throws Exception{

helloworld=(HelloWorld)EJBFactory.getEJB("HelloWorldBean/remote");

}

@Test

public void testSayHello(){

assertEquals("laozhang:这是我的第一个EJB3.",helloworld.SayHello("laozhang"));

}

}

//build.xml文件,编译和发布HelloWorldEJB到Jboss服务器
图片说明
图片说明
对HelloWorldTest类进行Junit测试结果抛出异常:

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

at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:674)

at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)

at javax.naming.InitialContext.init(InitialContext.java:244)

at javax.naming.InitialContext.(InitialContext.java:216)

at debug.EJBFactory.getEJB(EJBFactory.java:12)

at debug.HelloWorldTest.setUpBeforeClass(HelloWorldTest.java:12)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:497)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)

at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)

at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Caused by: java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory

at java.net.URLClassLoader.findClass(URLClassLoader.java:381)

at java.lang.ClassLoader.loadClass(ClassLoader.java:424)

at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)

at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Class.java:348)

at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:72)

at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:61)

at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:672)

... 20 more

我自己也在网上找了下问题所在,答案都是说环境变量没有配置好,或者将JBOSS_Home下client文件夹中的所有jar包都导入到项目工程中之类的,还有就是先建立一个ejb工程写接口和类然后导入另外的java工程里测试,我都试过了,但是都是上面的异常并没有什么作用。大神们帮帮我吧T.T,十分感谢!

2个回答

taiyangdao
taiyangdao   2016.04.25 23:25
已采纳

我看到你进行EJB调用的参数设置都还是过时的,请参考我的博文设置参数。
参考链接:http://blog.csdn.net/taiyangdao/article/details/50826865

taiyangdao
taiyangdao   2016.04.25 23:22

org.jnp.interfaces.NamingContextFactory是JBoss AS 7以前的类,已经过时了。
在WildFly AS 10中,请使用org.jboss.naming.remote.client.InitialContextFactory替换。

Csdn user default icon
上传中...
上传图片
插入图片