3 Replies Latest reply on Mar 8, 2001 9:01 AM by 3004

    Client code runnings as a NT service and InitialContext

    3004
      Has anyone run java code as an NT service and tried to get an
      InitialContext?

      Java code that runs fine from a command line, suddenly can't find the
      Context_Factory when running as a Service.

      The code below works fine when running from a command line. But does not
      work as a NT Service. The classpath is 100% correct, it's not a classpath
      issue, or so it seems. It has something to do when running as a service.

      By adding the following code:

      Thread t=Thread.currentThread();
      t.setContextClassLoader(this.getClass().getClassLoader());

      it works!!!!!!!

      An explanation of this behavior would be, to say the least, enlightening.

      Thanks,

      - Ed

      Stack Trace from Service mode

      javax.naming.NoInitialContextException: Cannot instantiate class:
      weblogic.jndi.WLInitialContextFactory. Root exception is
      java.lang.ClassNotFoundException: weblogic/jndi/WLInitialContextFactory
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:195)
      at
      com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:45)
      at
      javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:655)
      at
      javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:246)
      at javax.naming.InitialContext.init(InitialContext.java:222)
      at javax.naming.InitialContext.<init>(InitialContext.java:198)
      at Test.main(Test.java:47)
      Done


      Test.java
      cut
      import java.util.*;
      import javax.naming.*;
      import javax.naming.directory.*;
      import java.io.*;
      import java.util.jar.*;



      public final class Test {



      public static void main(String args[]) {

      String msgBody;

      if (args.length < 1)
      msgBody = "This is the message body!!";
      else
      msgBody = args[0];
      try {


      Properties prps = System.getProperties();
      Enumeration prp_enum = prps.keys();
      System.out.println("------ PROPERTIES -------");
      while (prp_enum.hasMoreElements()) {
      String key = prp_enum.nextElement().toString();
      System.out.println(key + " = " + prps.get(key));
      }


      Properties prop = new Properties();



      prop.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFact
      ory");
      prop.put(Context.PROVIDER_URL,"t3://localhost:80");


      //prop.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.jndi.CosNaming.CNInitial
      ContextFactory");
      //Saw this somewhere, don't know what this one does
      //env.put (Context.URL_PKG_PREFIXES, "oracle.aurora.jndi");



      //prop.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFa
      ctory");
      //prop.put(Context.PROVIDER_URL,"t3://localhost:80");

      InitialContext ic = new InitialContext(prop);
      } catch (Exception e) {
      e.printStackTrace();
      }


      System.out.println("Done");
      }


      }
      cut


        • 1. Client code runnings as a NT service and InitialContext
          3004
          Hi Ed,

          what are you doing to run your code as an NT Service? Are you using a
          launcher tool? Which one?

          Daniel

          -----Ursprüngliche Nachricht-----
          Von: Ed [mailto:longstrom.ed@atlascommerce.com]
          Bereitgestellt: Dienstag, 6. März 2001 22:52
          Bereitgestellt in: jndi
          Unterhaltung: Client code runnings as a NT service and InitialContext
          Betreff: Client code runnings as a NT service and InitialContext


          Has anyone run java code as an NT service and tried to get an
          InitialContext?

          Java code that runs fine from a command line, suddenly can't find the
          Context_Factory when running as a Service.

          The code below works fine when running from a command line. But does
          not
          work as a NT Service. The classpath is 100% correct, it's not a
          classpath
          issue, or so it seems. It has something to do when running as a
          service.

          By adding the following code:

          Thread t=Thread.currentThread();
          t.setContextClassLoader(this.getClass().getClassLoader());

          it works!!!!!!!

          An explanation of this behavior would be, to say the least,
          enlightening.

          Thanks,

          - Ed

          Stack Trace from Service mode

          javax.naming.NoInitialContextException: Cannot instantiate class:
          weblogic.jndi.WLInitialContextFactory. Root exception is
          java.lang.ClassNotFoundException: weblogic/jndi/WLInitialContextFactory
          at java.lang.Class.forName0(Native Method)
          at java.lang.Class.forName(Class.java:195)
          at
          com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:4
          5)
          at
          javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:655)
          at
          javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:246)
          at javax.naming.InitialContext.init(InitialContext.java:222)
          at javax.naming.InitialContext.<init>(InitialContext.java:198)
          at Test.main(Test.java:47)
          Done


          Test.java
          cut
          import java.util.*;
          import javax.naming.*;
          import javax.naming.directory.*;
          import java.io.*;
          import java.util.jar.*;



          public final class Test {



          public static void main(String args[]) {

          String msgBody;

          if (args.length < 1)
          msgBody = "This is the message body!!";
          else
          msgBody = args[0];
          try {


          Properties prps = System.getProperties();
          Enumeration prp_enum = prps.keys();
          System.out.println("------ PROPERTIES -------");
          while (prp_enum.hasMoreElements()) {
          String key = prp_enum.nextElement().toString();
          System.out.println(key + " = " + prps.get(key));
          }


          Properties prop = new Properties();



          prop.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContext
          Fact
          ory");
          prop.put(Context.PROVIDER_URL,"t3://localhost:80");


          //prop.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.jndi.CosNaming.CNIni
          tial
          ContextFactory");
          //Saw this somewhere, don't know what this one does
          //env.put (Context.URL_PKG_PREFIXES, "oracle.aurora.jndi");



          //prop.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialConte
          xtFa
          ctory");
          //prop.put(Context.PROVIDER_URL,"t3://localhost:80");

          InitialContext ic = new InitialContext(prop);
          } catch (Exception e) {
          e.printStackTrace();
          }


          System.out.println("Done");
          }


          }
          cut


          • 2. Re: Client code runnings as a NT service and InitialContext
            3004
            Yes,

            I am using jsrvany at this time. I spoke with the author briefly, but no
            conclusions were made. I also wrote my own "mini-launcher" which just
            basically is a vanilla service wrapper that executes the java.exe via
            vanilla JNI (right off sun's sight).

            Thanks,

            - Ed


            http://jsrvany.sourceforge.net/


            Daniel Hoppe <hoppe@sitewaerts.de> wrote in message
            news:923A7281CDB7D4118A5000500450BB08080936@samoa.sitewaerts...
            Hi Ed,

            what are you doing to run your code as an NT Service? Are you using a
            launcher tool? Which one?

            Daniel

            -----Ursprüngliche Nachricht-----
            Von: Ed [mailto:longstrom.ed@atlascommerce.com]
            Bereitgestellt: Dienstag, 6. März 2001 22:52
            Bereitgestellt in: jndi
            Unterhaltung: Client code runnings as a NT service and InitialContext
            Betreff: Client code runnings as a NT service and InitialContext


            Has anyone run java code as an NT service and tried to get an
            InitialContext?

            Java code that runs fine from a command line, suddenly can't find the
            Context_Factory when running as a Service.

            The code below works fine when running from a command line. But does
            not
            work as a NT Service. The classpath is 100% correct, it's not a
            classpath
            issue, or so it seems. It has something to do when running as a
            service.

            By adding the following code:

            Thread t=Thread.currentThread();
            t.setContextClassLoader(this.getClass().getClassLoader());

            it works!!!!!!!

            An explanation of this behavior would be, to say the least,
            enlightening.

            Thanks,

            - Ed

            Stack Trace from Service mode

            javax.naming.NoInitialContextException: Cannot instantiate class:
            weblogic.jndi.WLInitialContextFactory. Root exception is
            java.lang.ClassNotFoundException: weblogic/jndi/WLInitialContextFactory
            at java.lang.Class.forName0(Native Method)
            at java.lang.Class.forName(Class.java:195)
            at
            com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:4
            5)
            at
            javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:655)
            at
            javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:246)
            at javax.naming.InitialContext.init(InitialContext.java:222)
            at javax.naming.InitialContext.<init>(InitialContext.java:198)
            at Test.main(Test.java:47)
            Done


            Test.java
            cut
            import java.util.*;
            import javax.naming.*;
            import javax.naming.directory.*;
            import java.io.*;
            import java.util.jar.*;



            public final class Test {



            public static void main(String args[]) {

            String msgBody;

            if (args.length < 1)
            msgBody = "This is the message body!!";
            else
            msgBody = args[0];
            try {


            Properties prps = System.getProperties();
            Enumeration prp_enum = prps.keys();
            System.out.println("------ PROPERTIES -------");
            while (prp_enum.hasMoreElements()) {
            String key = prp_enum.nextElement().toString();
            System.out.println(key + " = " + prps.get(key));
            }


            Properties prop = new Properties();



            prop.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContext
            Fact
            ory");
            prop.put(Context.PROVIDER_URL,"t3://localhost:80");


            //prop.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.jndi.CosNaming.CNIni
            tial
            ContextFactory");
            //Saw this somewhere, don't know what this one does
            //env.put (Context.URL_PKG_PREFIXES, "oracle.aurora.jndi");



            //prop.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialConte
            xtFa
            ctory");
            //prop.put(Context.PROVIDER_URL,"t3://localhost:80");

            InitialContext ic = new InitialContext(prop);
            } catch (Exception e) {
            e.printStackTrace();
            }


            System.out.println("Done");
            }


            }
            cut

            • 3. Re: Client code runnings as a NT service and InitialContext
              3004
              Hi Ed,

              I guess I have an idea on what's going wrong:

              Unlike e.g. JDBC, JNDI does not classes with Class.forName() to load the
              InitialContextFactory. Instead it uses the context classloader. If this
              context classloader is not set to the classloader of your application,
              they might have different classpaths, and your InitialContextFactory
              won't be on the correct classpath.
              In a common standalone application, both classloaders finally point to
              the system classloader -> no problem.

              Now consider the following scenario:
              1. Your launcher has a custom classloader to load your application
              classes
              2. Your application classes are not on the system classpath
              3. Your launcher does not set your applications context classloader to
              the custom classloader

              If you try to create your InitialContext now, the context classloader
              points to the system classloader, which does not find the required
              classes. That's why calling
              Thread.currentThread().setContextClassLoader(getClass
              ().getClassLoader()) fixes the problem for you. You could probably check
              with the authors of your launcher, if they use some custom classloader,
              maybe they can fix this.

              Daniel



              -----Ursprüngliche Nachricht-----
              Von: Ed [mailto:longstrom.ed@atlascommerce.com]
              Bereitgestellt: Mittwoch, 7. März 2001 16:36
              Bereitgestellt in: jndi
              Unterhaltung: Client code runnings as a NT service and InitialContext
              Betreff: Re: Client code runnings as a NT service and InitialContext


              Yes,

              I am using jsrvany at this time. I spoke with the author briefly, but
              no
              conclusions were made. I also wrote my own "mini-launcher" which just
              basically is a vanilla service wrapper that executes the java.exe via
              vanilla JNI (right off sun's sight).

              Thanks,

              - Ed


              http://jsrvany.sourceforge.net/


              Daniel Hoppe <hoppe@sitewaerts.de> wrote in message
              news:923A7281CDB7D4118A5000500450BB08080936@samoa.sitewaerts...
              Hi Ed,

              what are you doing to run your code as an NT Service? Are you using a
              launcher tool? Which one?

              Daniel

              -----Ursprüngliche Nachricht-----
              Von: Ed [mailto:longstrom.ed@atlascommerce.com]
              Bereitgestellt: Dienstag, 6. März 2001 22:52
              Bereitgestellt in: jndi
              Unterhaltung: Client code runnings as a NT service and InitialContext
              Betreff: Client code runnings as a NT service and InitialContext


              Has anyone run java code as an NT service and tried to get an
              InitialContext?

              Java code that runs fine from a command line, suddenly can't find the
              Context_Factory when running as a Service.

              The code below works fine when running from a command line. But does
              not
              work as a NT Service. The classpath is 100% correct, it's not a
              classpath
              issue, or so it seems. It has something to do when running as a
              service.

              By adding the following code:

              Thread t=Thread.currentThread();
              t.setContextClassLoader(this.getClass().getClassLoader());

              it works!!!!!!!

              An explanation of this behavior would be, to say the least,
              enlightening.

              Thanks,

              - Ed

              Stack Trace from Service mode

              javax.naming.NoInitialContextException: Cannot instantiate class:
              weblogic.jndi.WLInitialContextFactory. Root exception is
              java.lang.ClassNotFoundException:
              weblogic/jndi/WLInitialContextFactory
              at java.lang.Class.forName0(Native Method)
              at java.lang.Class.forName(Class.java:195)
              at
              com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:4
              5)
              at
              javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:655)
              at
              javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:246)
              at javax.naming.InitialContext.init(InitialContext.java:222)
              at javax.naming.InitialContext.<init>(InitialContext.java:198)
              at Test.main(Test.java:47)
              Done


              Test.java
              cut
              import java.util.*;
              import javax.naming.*;
              import javax.naming.directory.*;
              import java.io.*;
              import java.util.jar.*;



              public final class Test {



              public static void main(String args[]) {

              String msgBody;

              if (args.length < 1)
              msgBody = "This is the message body!!";
              else
              msgBody = args[0];
              try {


              Properties prps = System.getProperties();
              Enumeration prp_enum = prps.keys();
              System.out.println("------ PROPERTIES -------");
              while (prp_enum.hasMoreElements()) {
              String key = prp_enum.nextElement().toString();
              System.out.println(key + " = " + prps.get(key));
              }


              Properties prop = new Properties();



              prop.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContext
              Fact
              ory");
              prop.put(Context.PROVIDER_URL,"t3://localhost:80");


              //prop.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.jndi.CosNaming.CNIni
              tial
              ContextFactory");
              //Saw this somewhere, don't know what this one does
              //env.put (Context.URL_PKG_PREFIXES, "oracle.aurora.jndi");



              //prop.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialConte
              xtFa
              ctory");
              //prop.put(Context.PROVIDER_URL,"t3://localhost:80");

              InitialContext ic = new InitialContext(prop);
              } catch (Exception e) {
              e.printStackTrace();
              }


              System.out.println("Done");
              }


              }
              cut