[英]InitialContext thrrows exception when trying lookup
我對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;
}
}
之前一切正常,我所做的就是更改項目名稱,現在它引發了異常。 我確實在lookup方法中更改了項目名稱,所以一切都應該正常工作,那么在這里我會丟失什么呢?
這是環境的問題,而不是代碼的問題(盡管代碼確實包含線索)。
您的代碼告訴jndi如何通過名為org.jboss.ejb.client.naming
的環境變量查找初始上下文。 該變量包含您的類的全名,以及其包名。 重命名項目時,您可能也更改了程序包名稱。 現在,環境變量org.jboss.ejb.client.naming
指向某個不存在的類。 您需要更改該變量以引用您重命名的類名稱,以解決此問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.