Question

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:

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?

Was it helpful?

Solution

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. 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. You need to change that variable to reference your renamed class name to fix the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top