在WildFly AS 10上部署EJB服务,在Eclispe中运行单元测试,遇到错误及其解决方法。
异常一:
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662) at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313) at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350) at javax.naming.InitialContext.lookup(InitialContext.java:417)
解决办法:将%WildFly%/bin/jboss-client.jar加入到buildclasspath
异常二:
java.lang.NoSuchMethodError: org.jboss.logging.Logger.getMessageLogger(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Object; at org.jboss.ejb.client.Logs.<clinit>(Logs.java:53) at org.jboss.ejb.client.naming.ejb.EjbNamingContext.createEjbProxy(EjbNamingContext.java:188) at org.jboss.ejb.client.naming.ejb.EjbNamingContext.lookup(EjbNamingContext.java:176) at javax.naming.InitialContext.lookup(InitialContext.java:417)
JDK 1.8
来自WildFly的jboss-client.jar
log4j-1.2.jar
...
经过排查,发现classpath下还有来自JBoss AS的jbossall-client.jar,该文件中包含了过时org.jboss.logging.Logger类,导致异常。
将其从classpath中删除即可。
异常三:
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662) at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313) at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350) at javax.naming.InitialContext.lookup(InitialContext.java:417)解决办法:JNDI名称必须以"ejb:..."的形式,如 " ejb: EthernetPM/EthernetPMEJB/EthernetPMServiceBean!com.jdsu.netcomplete.service.ethernetpm.EthernetPMServiceRemote"
异常四:
java.lang.IllegalStateException: EJBCLIENT000025: No EJB receiver available for handling [appName:CustomView, moduleName:SecurityDelegateEJB, distinctName:] combination for invocation context org.jboss.ejb.client.EJBClientInvocationContext@12e5a8e at org.jboss.ejb.client.EJBClientContext.requireEJBReceiver(EJBClientContext.java:798) at org.jboss.ejb.client.ReceiverInterceptor.handleInvocation(ReceiverInterceptor.java:116) at org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:186) at org.jboss.ejb.client.EJBInvocationHandler.sendRequestWithPossibleRetries(EJBInvocationHandler.java:255) at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:200) at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:183) at org.jboss.ejb.client.EJBInvocationHandler.invoke(EJBInvocationHandler.java:146)这是EJB调用的经典问题,常常被遇到,常常被忽略。