Discussions

EJB programming & troubleshooting: NoIntialContextException

  1. NoIntialContextException (7 messages)

    i have tried creating my first ejb. i created all the classes, downloaded the j2ee reference implementation from sun. i used the deploy tool to create the application with the bean and deployed it. i then created the client application to try using it. but.... its giving me this error.

    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 users.derekv.CabinClient.main(CabinClient.java:19)



    here is my client code.



    public class CabinClient {
      
      public static void main(String[] args) {
        try {
          Context jndiContext = getInitialContext();
          Object ref = jndiContext.lookup("java:comp/env/ejb/Cabin");
          CabinHome home = (CabinHome)PortableRemoteObject.narrow(ref, CabinHome.class);
          Cabin cabin = home.create(1);
          cabin.setName("Master Suite");
          cabin.setDeckLevel(1);
          cabin.setShip(1);
          cabin.setBedCount(3);
          
          CabinPK pk = new CabinPK();
          pk.id = 1;
          
          cabin = home.findByPrimaryKey(pk);
          System.out.println(cabin.getName());
          System.out.println(cabin.getDeckLevel());
          System.out.println(cabin.getShip());
          System.out.println(cabin.getBedCount());
          
        }
        catch (Exception e) {
          e.printStackTrace();
        }
      }
      
      public static Context getInitialContext() throws NamingException {
        return new javax.naming.InitialContext();
      }
    }

    i have read through all (i think) docs that came with j2ee, and it says something about setting environment variables so that the initial context will be able to connect to the naming service and find the ejb object. but, either i dont understand it, or i'm just plain missing something. i've cruised around sun's site and some other sites as well with no luck... ANY help at this point would be greatly appreciated. thanks.

    Threaded Messages (7)

  2. NoIntialContextException[ Go to top ]

    just enter import <classname(cabin,intrfaces)> at the begining
  3. NoIntialContextException[ Go to top ]

    i do have the import statments, i didnt include them in my question. the program compiles fine. i get the error when i run the program.
  4. NoIntialContextException[ Go to top ]

    have u given any jndi name to cabin.if yes in the lookup method give that name.
  5. NoIntialContextException[ Go to top ]

    yes i have given cabin a name, as you can see from the code that i included in my question..... read the code where it calls the method "lookup".
  6. NoIntialContextException[ Go to top ]

    Hey Manish
        If u dont know things..its not necessary to give bullshit.its better to keep ur trap shut :-)
    lkbm
  7. NoIntialContextException[ Go to top ]

    well i think i figured out what i needed to do, i needed to add more classes to my CLASSPATH. i added every jar and its brother to my classpath and it worked. oh well....
  8. NoIntialContextException[ Go to top ]

    Hi,
    I jsut started using J2EE Server which I downloaded from Javasoft.com. I was trying to run the same example and getting this error. I added all the .jar files in classpath but still getting this error.

    javax.naming.NoInitialContextException: Need to specify class name in environmen
    t or system property, or as an applet parameter, or in an application resource f
    ile: java.naming.factory.initial
            at java.lang.Throwable.<init>(Compiled Code)
            at java.lang.Exception.<init>(Compiled Code)
            at javax.naming.NamingException.<init>(Compiled Code)
            at javax.naming.NoInitialContextException.<init>(Compiled Code)
            at javax.naming.spi.NamingManager.getInitialContext(Compiled Code)
            at javax.naming.InitialContext.getDefaultInitCtx(Compiled Code)
            at javax.naming.InitialContext.getURLOrDefaultInitCtx(Compiled Code)
            at javax.naming.InitialContext.lookup(Compiled Code)
            at ConverterClient.main(Compiled Code)


    Could you please anyone tell me what exactly is the problem. I have checked the lookup method and also deployed correctly as per documentation.

    Here is the client code.....


    ***************************

    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.rmi.PortableRemoteObject;

    import Converter;
    import ConverterHome;

    public class ConverterClient {

       public static void main(String[] args) {
           try {
               Context initial = new javax.naming.InitialContext();
               Object objref = initial.lookup("java:MyConverter");

               ConverterHome home =
                   (ConverterHome)PortableRemoteObject.narrow(objref,
                                                ConverterHome.class);

               Converter currencyConverter = home.create();
               double amount = currencyConverter.dollarToYen(100.00);
               System.out.println(String.valueOf(amount));
               amount = currencyConverter.yenToEuro(100.00);
               System.out.println(String.valueOf(amount));

           } catch (Exception ex) {
               System.err.println("Caught an unexpected exception!");
               ex.printStackTrace();
           }
       }
    }

    *******************************************

    Please reply as soon as you can.
    Thanks in advance.