2 Replies Latest reply: Apr 23, 2012 3:52 AM by gimbal2 RSS

    javax.naming.NoInitialContextException...

    Joseph Hwang
      I use ejb3.1 on JBoss 7.0.2. I develop bean with @Schedule annotation. The codes are :


      ==== IScheduleTestPort.java

      package com.aaa.ejb3;

      import javax.ejb.Remote;

      @Remote
      public interface IScheduleTestPort {
      public String getSignal();
      }


      ===== ScheduleBean.java

      package com.aaa.ejb3;

      import java.util.Date;
      import javax.annotation.PostConstruct;
      import javax.ejb.Schedules;
      import javax.ejb.Schedule;
      import javax.ejb.Stateless;

      @Stateless
      public class ScheduleBean implements IScheduleTestPort {

      private String str;

      @PostConstruct
      public void doPostConstruct() {
      str = "Time is : ";
      }

      @Schedules({
      @Schedule(dayOfMonth = "5", hour = "09-18", minute = "*", second="10/5", timezone = "America/Los_Angeles"),
      @Schedule(dayOfMonth = "Mon", hour = "10-17", minute = "*", second="*/5" , timezone = "Asia/Seoul") })
      @Override
      public String getSignal() {
      // TODO Auto-generated method stub
      return str + new Date();
      }

      }

      ===== ScheduleBeanClient.java

      package com.aaa.ejb3;

      import java.util.Properties;
      import javax.naming.InitialContext;
      import javax.naming.Context;

      public class ScheduleBeanClient {

      /**
      * @param args
      */
      public static void main(String[] args) {

      // TODO Auto-generated method stub
      try {
      Properties props = new Properties();

      props.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
      props.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
      props.put("java.naming.provider.url", "jnp://localhost:1099");

      Context ctx = new InitialContext(props);
      ScheduleBean schedule = (ScheduleBean)ctx.lookup("java:module/ScheduleBean");

      while (true) {
      System.out.println(schedule.getSignal());
      }
      } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }

      }

      }


      But client throws exception like below :


      javax.naming.NoInitialContextException: Cannot instantiate class: org.jnp.interfaces.NamingContextFactory [Root exception is java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory]

      at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:657)
      at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
      at javax.naming.InitialContext.init(InitialContext.java:223)
      at javax.naming.InitialContext.<init>(InitialContext.java:197)
      at com.aaa.ejb3.ScheduleBeanClient.main(ScheduleBeanClient.java:47)

      Caused by: java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory
      at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:247)
      at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:46)
      at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:654)


      So i remove the parameter of InitialContext,


      public static void main(String[] args) { 

      // TODO Auto-generated method stub
      try {

      Context ctx = new InitialContext();
      ScheduleBean schedule = (ScheduleBean)ctx.lookup("java:module/ScheduleBean");


      while (true) {
      System.out.println(schedule.getSignal());
      }
      } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }

      ...


      This the codes throws the following 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(NamingManager.java:645)
      at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
      at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
      at javax.naming.InitialContext.lookup(InitialContext.java:392)
      at com.aaa.ejb3.ScheduleBeanClient.main(ScheduleBeanClient.java:49)

      I have no idea of what is wrong with my codes. I need your help!

      Thanks in advanced.
        • 1. Re: javax.naming.NoInitialContextException...
          EJP
          But client throws exception like below :


          javax.naming.NoInitialContextException: Cannot instantiate class: org.jnp.interfaces.NamingContextFactory [Root exception is java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory]
          So you need to add that class to your CLASSPATH.
          So i remove the parameter of InitialContext
          So you did exactly the wrong thing.
          • 2. Re: javax.naming.NoInitialContextException...
            gimbal2
            Joseph wrote:
            I have no idea of what is wrong with my codes. I need your help!
            Well then get an idea. Read the JBoss 7 documentation on how to create EJB client applications. The JBoss 7 documentation is quite complete which is a rare treat; make good use of that blessing. Here, the google search I did ("jboss 7 ejb client"), the first hit pointed me towards the correct document:

            https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI

            Take special note to what is listed under "Setting up the client classpath with the jars that are required to run the client application". I mean I'm almost crying here, the documentation is beautiful and you're not even using it :(

            NOTE: better to use JBoss 7.1 when you use the EJB scheduler. In JBoss 7.0 it is not implemented fully.