简体   繁体   English

尝试查找时InitialContext引发异常

[英]InitialContext thrrows exception when trying lookup

I'm pretty new to EJB , and I'm trying to lookup a EJB from the InitialContext , Every thing was working fine , Until I changed the project name and now its giving me this exception: 我对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)

Here is my code: 这是我的代码:

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;
}

}

It was working fine before, All I did was change the project name and now it throws the exception. 之前一切正常,我所做的就是更改项目名称,现在它引发了异常。 I did change the project name in the lookup method so everything should have worked , So what could I be missing here? 我确实在lookup方法中更改了项目名称,所以一切都应该正常工作,那么在这里我会丢失什么呢?

It is a problem with your environment, not with your code (although the code does contain a clue). 这是环境的问题,而不是代码的问题(尽管代码确实包含线索)。

Your code tells jndi how to look up the initial context through an environment variable called org.jboss.ejb.client.naming . 您的代码告诉jndi如何通过名为org.jboss.ejb.client.naming的环境变量查找初始上下文。 The variable contains a full name of your class, complete with its package name. 该变量包含您的类的全名,以及其包名。 When you renamed the project, you probably changed the package name as well. 重命名项目时,您可能也更改了程序包名称。 Now the environment variable org.jboss.ejb.client.naming points to some class that does not exist. 现在,环境变量org.jboss.ejb.client.naming指向某个不存在的类。 You need to change that variable to reference your renamed class name to fix the problem. 您需要更改该变量以引用您重命名的类名称,以解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM