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服务器
<?xml version="1.0" encoding="UTF-8"?>
<!-- EJB3 HelloWorld build file -->
<project name="HelloworldEJB" default="ejbjar" basedir=".">
<property environment="env"/>
<property name="app.dir" value="${basedir}"/>
<property name="src.dir" value="${app.dir}/src"/>
<property name="jboss.home" value="${env.jboss_home}"/>
<property name="jboss.server.config" value="all"/>
<property name="build.dir" value="${app.dir}/build"/>
<property name="build.classes.dir" value="${build.dir}/classes"/>
<property name="ejbjar.name" value="${ant.project.name}"/>
<!--Build classpath-->
<path id="build.classpath">
<fileset dir="F:\wildfly-10.0.0.Final\bin\client">
<include name="*.jar"/>
</fileset>
<pathelement location ="${build.classes.dir}"/>
</path>
<!-- Prepares the build directory -->
<target name="prepare" depends="clean">
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.classes.dir}"/>
</target>
<!-- Compiles the source code -->
<target name="compile" depends="prepare" description="编译">
<Javac srcdir="${src.dir}" destdir="${build.classes.dir}" debug="on" deprecation="on" potimize="off" includes="edu/**">
</Javac>
</target>
<target name="ejbjar" depends="compile" description="创建EJB发布包">
<jar jarfile="${ap.dir}/${ejbjar.name}.jar">
<fileset dir="${build.classes.dir}">
<include name="**/*.class"/>
<exclude name="junit/debug/*.*"/>
</fileset>
</jar>
</target>
<target name="deploy" depends="ejbjar" description="发布EJB">
<copy file="${app.dir}/${ejbjar.name}.jar" todir="${jboss.home}/server/${jboss.server.config}/deploy"/>
</target>
<!-- Clean up generated stuff -->
<target name="clean">
<delete dir="${build.dir}" description="卸载EJB"/>
<delete file="${jboss.home}/server/${jboss.server.config}/deploy/${ejbjar.name}.jar"/>
</target>
</project>
对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.<init>(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,十分感谢!
Img_cache
相关分类