7 Replies Latest reply on May 17, 2008 8:59 AM by joseph pareti

    javax.naming.NoInitialContextException: Can't find property:

    joseph pareti Newbie

      I am developing a demo based on JBOSS 4.2.0.CR2 on Linux RHEL 3/x86.
      The demo includes an EJB (stateless session bean), and a client app that requests services from the bean.

      I invariably get the following rt error when launching the client as
      # ant runClientApp

      [java] javax.naming.NoInitialContextException: Can't find property: java.naming.factory.initial
      [java] at javax.naming.spi.NamingManager.getInitialContext(j ava.util.Hashtable) (/lib/ssa/libgcj.so.4.0.0)
      [java] at javax.naming.InitialContext.getDefaultInitCtx() (/lib/ssa/libgcj.so.4.0.0)
      [java] at javax.naming.InitialContext.getURLOrDefaultInitCtx (java.lang.String) (/lib/ssa/libgcj.so.4.0.0)
      [java] at javax.naming.InitialContext.lookup(java.lang.Strin g) (/lib/ssa/libgcj.so.4.0.0)
      [java] at za.co.solms.finance.calculators.LoanCalculatorPane l.connect() (Unknown Source)
      [java] at za.co.solms.finance.calculators.LoanCalculatorPane l.init() (Unknown Source)
      [java] at za.co.solms.finance.calculators.LoanCalculatorPane l.LoanCalculatorPanel() (Unknown Source)
      [java] at za.co.solms.finance.calculators.LoanCalculatorClie nt.LoanCalculatorClient() (Unknown Source)
      [java] at za.co.solms.finance.calculators.LoanCalculatorClie nt.main(java.lang.String[]) (Unknown Source)

      my jndi.properties, available in classpath and also under $JAVA_HOME, is as follows:
      (I also saw jndi.properties is included in the client jar file, so I assume it is taken into account):

      java.naming.factory.initial=org.jnp.interfaces.Nam ingContextFactory
      java.naming.provider.url=jnp://localhost:1099
      java.naming.factory.url.pkgs=org.jboss.namingrg. jnp.interfaces
      jnp.socketFactory=org.jnp.interfaces.TimedSocketFactory
      #Connection timeout (0 == blocking)
      jnp.timeout=0
      #Read timeout (0 == blocking)
      jnp.sotimeout=0

      Finally, I started playing around with the client code by e.g. changing the name of the bean in the lookup function ...

      Object beanHomeRef
      = jndiContext.lookup(jndiName);

      into

      Object beanHomeRef
      = jndiContext.lookup("local/ejb/LoanCalculator");

      but without any results

        • 1. Re: javax.naming.NoInitialContextException: Can't find prope
          wayne baylor Apprentice

          just for kicks, you could pass InitialContext a Hashtable with your jndi.properties hardcoded. if that works, then you know it's something wrong with your classpath.

          ps. you lookup string looks funny, by default it's "[ear-name/]bean-name/local"

          • 2. Re: javax.naming.NoInitialContextException: Can't find prope
            jaikiran pai Master

            As waynebaylor mentioned, the most likely reason is that the jndi.properties file is not being found in the classpath. Try out waynebaylor's suggestion. Furthermore, which version and implementation of Java are you using? When you start JBoss, it prints out the JDK information. I use JDK 5 from Sun and see this output when the server is starting:

            16:37:22,464 INFO [ServerInfo] Java version: 1.5.0_07,Sun Microsystems Inc.
            16:37:22,480 INFO [ServerInfo] Java VM: Java HotSpot(TM) Server VM 1.5.0_07-b03,Sun Microsystems Inc.
            16:37:22,480 INFO [ServerInfo] OS-System: Windows 2003 5.2,x86


            What do you see on your system?

            Also, you mention that your jndi.properties contains:

            java.naming.factory.initial=org.jnp.interfaces.Nam ingContextFactory


            I see a space between letters in "NamingContextFactory". Is this a typo while posting in the forum or is this the actual content in your jndi.properties file?

            • 3. Re: javax.naming.NoInitialContextException: Can't find prope
              joseph pareti Newbie

              After a long time I am revisiting this topic and even though I tried to implement your advise the very same error message stays:

              javax.naming.NoInitialContextException: Can't find property: java.naming.factory.initial

              Below are the details on what I did. TIA for any further help.

              SOURCE CODE MODIFICATIONS

              package za.co.solms.finance.calculators;
              import javax.naming.Context;
              import java.util.Hashtable;
              import javax.naming.InitialContext;
              import javax.rmi.PortableRemoteObject;
              import swingwt.awt.*;
              import java.text.*;
              import java.util.*;
              import swingwtx.swing.*;
              import swingwt.awt.event.*;
              public class LoanCalculatorClient extends JFrame
              {
              public LoanCalculatorClient()
              {
              setTitle("Solms Training Loan Calculator");
              java.util.Hashtable properties = new java.util.Hashtable();
              properties.put(Context.PROVIDER_URL,"localhost:1099");
              properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
              properties.put("java.naming.rmi.security.manager", "yes");
              /*env.put(Context.URL_PKG_PREFIXES,"org.jboss.naming.client:org.jnp.interfaces");*/
              /* this is default env.put(jnp.socketFactory,"org.jnp.interfaces.TimedSocketFactory"); */
              /* Context initialContext = new InitialContext(env); */
              javax.naming.Context context = new javax.naming.InitialContext(properties);

              calculatorPanel = new LoanCalculatorPanel();
              getContentPane().add(calculatorPanel);
              addWindowListener(new WindowAdapter()
              {
              public void windowClosing(WindowEvent event)
              {
              calculatorPanel.destroy();
              System.exit(0);
              }
              });
              pack();
              }
              public static void main(String[] args)

              {
              new LoanCalculatorClient().show();
              }
              private LoanCalculatorPanel calculatorPanel;
              }

              =======

              public void connect()
              {
              try
              {
              InitialContext jndiContext = new InitialContext();
              System.out.println("Now looking up session bean " + jndiName + " ...");
              java.util.Hashtable properties = new java.util.Hashtable();
              properties.put(Context.PROVIDER_URL,"localhost:1099");
              properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
              properties.put("java.naming.rmi.security.manager", "yes");
              javax.naming.Context context = new javax.naming.InitialContext(properties);

              Object beanHomeRef
              = jndiContext.lookup(jndiName);
              System.out.println("got it");
              LoanCalculatorHome home =
              (LoanCalculatorHome)PortableRemoteObject.narrow
              (beanHomeRef, LoanCalculatorHome.class);
              loanCalculator = home.create();
              }

              ERRORS IN THE LOG FILE FROM BUILDING THE CLIENT JAR

              /usr/local/samba/tmp/jboss/jboss_demo_solms/src/client/application/LoanCalculatorClient.java:17: error: Undefined variable or class name: `java.naming.factory.initial'.
              [javac] env.put(java.naming.factory.initial,"org.jnp.interfaces.NamingContextFactory");
              [javac] ^
              [javac]

              This has been fixed by using:

              properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");

              in lieu of:

              properties.put(java.naming.factory.initial,"org.jnp.interfaces.NamingContextFactory");

              This may be a coincidence, but the client run-time error also refers to "java.naming.factory.initial" ; so I am starting to suspect a more fundamental problem with java.



              ALTERNATIVE WAYS TO ACCESS THE JNDI SERVICE?

              Because I think the following java statement causes the exception:

              Object beanHomeRef
              = jndiContext.lookup(jndiName);

              I am wondering if one could use instead something like:

              Object beanHomeRef
              = jndiContext.lookup("java:/path");

              Or

              Java.lang.Object ejbHome =
              = initialContext.lookup("ava:/path");


              WARNINGS IN THE LOG FILE FROM BUILDING THE CLIENT JAR

              I don't know how serious the following are:

              [javac] Using gcj compiler
              dropping /usr/local/samba/tmp/jboss/jboss_demo_solms/${classpath} from path as it doesn't exist

              and

              Property ${classpath} has not been set

              - is classpath case sensitive?
              - I know from a previous experience that classpath was fixed by just listing jar files; in this case it does indeed contain jar files AND regular directories ???

              • 4. Re: javax.naming.NoInitialContextException: Can't find prope
                Kazuhisa Ichikawa Newbie

                 

                InitialContext jndiContext = new InitialContext();
                System.out.println("Now looking up session bean " + jndiName + " ...");
                java.util.Hashtable properties = new java.util.Hashtable();
                properties.put(Context.PROVIDER_URL,"localhost:1099");
                properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
                properties.put("java.naming.rmi.security.manager", "yes");
                javax.naming.Context context = new javax.naming.InitialContext(properties);
                Object beanHomeRef
                = jndiContext.lookup(jndiName);
                


                I wonder if the the last line really should be:

                Object beanHomeRef
                = context.lookup(jndiName);
                


                because when you construct jndiConext, you're not passing the properties as argument:

                InitialContext jndiContext = new InitialContext();


                rather you do that for context,

                javax.naming.Context context = new javax.naming.InitialContext(properties);
                



                • 5. Re: javax.naming.NoInitialContextException: Can't find prope
                  joseph pareti Newbie

                  I have made the following code change (see statement in bold), because the expression involving = new InitialContext() should in my opinion follow the proerties settings, however that didn't solve the problem.

                  kahzoo-san; do you mean the statement should be:

                  Object beanHomeRef = context.lookup();


                  or should I use ?

                  InitialContext=jndiContext = new InitialContext(properties);
                  =======


                  public void connect()
                  {
                  try
                  {
                  
                  System.out.println("Now looking up session bean " + jndiName + " ...");
                  java.util.Hashtable properties = new java.util.Hashtable();
                  properties.put(Context.PROVIDER_URL,"localhost:1099");
                  properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
                  properties.put("java.naming.rmi.security.manager", "yes");
                  javax.naming.Context context = new javax.naming.InitialContext(properties);
                  InitialContext jndiContext = new InitialContext();
                  Object beanHomeRef
                  = jndiContext.lookup(jndiName);
                  System.out.println("got it");
                  LoanCalculatorHome home =
                  (LoanCalculatorHome)PortableRemoteObject.narrow
                  (beanHomeRef, LoanCalculatorHome.class);
                  loanCalculator = home.create();
                  }


                  • 6. Re: javax.naming.NoInitialContextException: Can't find prope
                    Kazuhisa Ichikawa Newbie

                    Sorry if my previous post was not clear enough.

                    I suggested to change the code as follows:

                    public void connect()
                    {
                    try
                    {
                    
                    System.out.println("Now looking up session bean " + jndiName + " ...");
                    java.util.Hashtable properties = new java.util.Hashtable();
                    properties.put(Context.PROVIDER_URL,"localhost:1099");
                    properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
                    properties.put("java.naming.rmi.security.manager", "yes");
                    javax.naming.Context context = new javax.naming.InitialContext(properties);
                    
                    Object beanHomeRef
                     = context.lookup(jndiName);
                    
                    System.out.println("got it");
                    LoanCalculatorHome home =
                    (LoanCalculatorHome)PortableRemoteObject.narrow
                    (beanHomeRef, LoanCalculatorHome.class);
                    loanCalculator = home.create();
                    }
                    


                    • 7. Re: javax.naming.NoInitialContextException: Can't find prope
                      joseph pareti Newbie

                      thanks Kazuhisa-san, I have not yet tried your latest suggestion, but I'd like to point out some strange messages from Jboss upon start up (via executing run.sh):

                      09:32:17,947 WARN [ServiceController] Problem starting service jboss:service=NamingBeanImpl
                      javax.naming.NamingException: Failed to find j2ee.clientName in jndi env
                       at org.jboss.naming.client.java.javaURLContextFactory.getObjectInstance(javaURLContextFactory.java:81)
                      
                      ...
                      
                      09:32:31,310 ERROR [UUIDKeyGeneratorFactoryService] Caught exception during startService()
                      javax.naming.CommunicationException: Could not obtain connection to any of these urls: localhost:1099 and discovery failed with error: javax.naming.CommunicationException: No such device [Root exception is java.net.SocketException: No such device] [Root exception is javax.naming.CommunicationException: Failed to connect to server localhost:1099 [Root exception is javax.naming.ServiceUnavailableException: Failed to connect to server localhost:1099 [Root exception is java.net.ConnectException: Connection refused]]]
                       at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1414)
                       at org.jnp.interfaces.NamingContext.rebind(NamingContext.java:484)
                       at org.jnp.interfaces.NamingContext.rebind(NamingContext.java:477)
                      ...