我是EJB的新手,我正在尝试从InitialContext中查找EJB,每件事都运行正常,直到我更改了项目名称,现在它给了我这个例外:
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(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at client.TestingEnt.main(TestingEnt.java:23)
这是我的代码:
public class TestingEnt {
public static void main(String[] args){
InitialContext ctx=getInitialContext();
EmployeeManager stub=null;
try {
stub = (EmployeeManager) ctx.lookup(getLookupName());
} catch (NamingException e) {
System.out.println("Lookup Failed");
e.printStackTrace();
}
//Add new employee
Employee emp1=new Employee();
emp1.setFirstName("David");
emp1.setLastName("Young");
emp1.setDept("Sales");
emp1.setSalary(10000);
stub.addEmployee(emp1);
System.out.println("Employee no'1 was added");
Employee emp2=new Employee();
emp2.setFirstName("Dana");
emp2.setLastName("Kore");
emp2.setDept("Finance");
emp2.setSalary(15000);
stub.addEmployee(emp2);
System.out.println("Employee no'2 was added");
System.out.println("______________________________");
private static String getLookupName() {
String appName = "";
String moduleName = "EntitiesLab";
String distinctName = "";
String beanName = EmployeeManagerBean.class.getSimpleName();
System.out.println(beanName);
final String interfaceName = EmployeeManager.class.getName();
System.out.println(interfaceName);
String name = "ejb:" + appName + "/" + moduleName + "/" +
distinctName + "/" + beanName + "!" + interfaceName;
System.out.println(name);
return name;
}
public static InitialContext getInitialContext(){
Properties properties = new Properties();
properties.put(Context.URL_PKG_PREFIXES,"org.jboss.ejb.client.naming");
try {
return new InitialContext(properties);
} catch (NamingException e) {
System.out.println("Cannot generate InitialContext");
e.printStackTrace();
}
return null;
}
}
以前工作正常,我所做的只是更改项目名称,现在它抛出了异常。我确实在查找方法中更改了项目名称,所以一切都应该有效,那么我可以在这里找到什么呢?
答案 0 :(得分:1)
这是您的环境的问题,而不是您的代码(尽管代码确实包含线索)。
您的代码告诉jndi如何通过名为org.jboss.ejb.client.naming
的环境变量查找初始上下文。该变量包含类的全名,包含其包名。重命名项目时,您可能也更改了包名称。现在环境变量org.jboss.ejb.client.naming
指向一些不存在的类。您需要更改该变量以引用重命名的类名来解决问题。