我在运行简单的EJB3 cient访问会话bean时收到javax.naming.NoInitialContextException
。尝试访问会话bean时出错
下面是我的客户端类:
package com.ibytecode.client;
import javax.naming.Context;
import javax.naming.NamingException;
import com.ibytecode.business.HelloWorld;
import com.ibytecode.businesslogic.HelloWorldBean;
import com.ibytecode.clientutility.ClientUtility;
public class EJBApplicationClient {
/**
* @param args
*/
public static void main(String[] args) {
HelloWorld bean = doLookup();
System.out.println(bean.sayHello()); // 4. Call business logic
}
private static HelloWorld doLookup() {
Context context = null;
HelloWorld bean = null;
try {
// 1. Obtaining Context
context = ClientUtility.getInitialContext();
// 2. Generate JNDI Lookup name
String lookupName = getLookupName();
// 3. Lookup and cast
bean = (HelloWorld) context.lookup(lookupName);
} catch (NamingException e) {
e.printStackTrace();
}
return bean;
}
private static String getLookupName() {
/*
The app name is the EAR name of the deployed EJB without .ear suffix.
Since we haven't deployed the application as a .ear,
the app name for us will be an empty string
*/
String appName = "";
/* The module name is the JAR name of the deployed EJB
without the .jar suffix.
*/
String moduleName = "HelloWorldSessionBean";
/*AS7 allows each deployment to have an (optional) distinct name.
This can be an empty string if distinct name is not specified.
*/
String distinctName = "";
// The EJB bean implementation class name
String beanName = HelloWorldBean.class.getSimpleName();
// Fully qualified remote interface name
final String interfaceName = HelloWorld.class.getName();
// Create a look up string name
String name = "ejb:" + appName + "/" + moduleName + "/" +
distinctName + "/" + beanName + "!" + interfaceName;
return name;
}
}
下面是我得到的错误:
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:307)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at com.ibytecode.client.EJBApplicationClient.doLookup(EJBApplicationClient.java:28)
at com.ibytecode.client.EJBApplicationClient.main(EJBApplicationClient.java:15)
Exception in thread "main" java.lang.NullPointerException
at com.ibytecode.client.EJBApplicationClient.main(EJBApplicationClient.java:16)
你是如何开始你的应用程序?你是将它作为独立的Java应用程序还是在应用程序客户端中运行? – marcus
嗨马库斯, 感谢您的时间。我作为一个独立的Java应用程序运行它。 –
你使用什么服务器?显示你的ClientUtility.getInitialContext()。我猜这是需要添加'props.put(Context.URL_PKG_PREFIXES,“org.jboss.ejb.client.naming”);' – Qwertovsky