1 Reply Latest reply: Jun 30, 2011 1:52 AM by r035198x RSS

    Issues while trying to create JNDI Context in Glassfish

    872285
      I believe I have searched this all across google but I didn't find a satisfactory answer so posting here to ask the question.

      /*
      * Create a JNDI API InitialContext object if none exists
      * yet.
      */
      try {
      jndiContext = new InitialContext();
      } catch (NamingException e) {
      System.out.println("Could1 not create JNDI API " +
      "context: " + e.toString());
      e.printStackTrace();
      System.exit(1);
      } catch (Exception e) {
      System.out.println("Exception. Could not create JNDI API " +
      "context: " + e.toString());
      e.printStackTrace();
      System.exit(1);
      }
      System.out.println("JNDI Created");

      Above is the code that is throwing exceptions

      Could1 not create JNDI API context: javax.naming.NoInitialContextException: Cann
      ot instantiate class: com.sun.enterprise.naming.impl.SerialInitContextFactory [R
      oot exception is java.lang.NullPointerException]
      javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.enterp
      rise.naming.impl.SerialInitContextFactory [Root exception is java.lang.NullPointerException]
           at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
           at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
           at javax.naming.InitialContext.init(Unknown Source)
           at javax.naming.InitialContext.<init>(Unknown Source)
           at examples.simple.SimpleQueueSender.main(SimpleQueueSender.java:93)

      Caused by: java.lang.NullPointerException
           at com.sun.enterprise.module.single.ManifestProxy.<init>(ManifestProxy.java:75)
           at com.sun.enterprise.module.single.ProxyModuleDefinition.<init>(ProxyModuleDef
      inition.java:78)
           at com.sun.enterprise.module.single.ProxyModuleDefinition.<init>(ProxyModuleDef
      inition.java:73)
           at com.sun.enterprise.module.single.SingleModulesRegistry.<init>(SingleModulesR
      egistry.java:79)
           at com.sun.enterprise.module.single.SingleModulesRegistry.<init>(SingleModulesR
      egistry.java:67)
           at com.sun.enterprise.module.single.StaticModulesRegistry.<init>(StaticModulesR
      egistry.java:66)
           at org.glassfish.internal.api.Globals.getStaticHabitat(Globals.java:84)
           at com.sun.enterprise.naming.impl.SerialInitContextFactory.<init>(SerialInitCon
      textFactory.java:236)
           at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
           at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
           at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
           at java.lang.reflect.Constructor.newInstance(Unknown Source)
           at java.lang.Class.newInstance0(Unknown Source)
           at java.lang.Class.newInstance(Unknown Source)
           ... 5 more


      Earlier while researching a previous issue, ManifestProxy was earlier not able to find "findResouces" method so with older versions of jar it was giving a different issue. I checked the latest version, not sure how the change in code could have resolved the issue. Seems earlier the error was NoMethodFoundException and now that has been caught and now it is throwing NullPointerException.

      Did any of you face this issue and can help? I have all the classes from GLASSFISH_HOME/lib and GLASSFISH_HOME/module in my classpath.
        • 1. Re: Issues while trying to create JNDI Context in Glassfish
          r035198x
          If you are running that code outside the container then you need to provide properties for the context factory with
            Properties props = new Properties();
            props.setProperty("java.naming.factory.initial","com.sun.enterprise.naming.SerialInitContextFactory");
            props.setProperty("java.naming.factory.url.pkgs","com.sun.enterprise.naming");
            props.setProperty("java.naming.factory.state","com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
            // optional    
           props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
           props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
           jndiContext  = new InitialContext(props);
          and make sure the appserv-rt.jar is on the classpath (by including it's path and name rather than by copying it to somewhere).