EJB programming & troubleshooting: javax.naming.NoInitialContextException

  1. Hi All

    I am getting this exception
    pls let me the solution

    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(

          at javax.naming.InitialContext.getDefaultInitCtx(

          at javax.naming.InitialContext.getURLOrDefaultInitCtx(

          at javax.naming.InitialContext.lookup(

          at DAOAdapter.main(

  2. Please post the code that throws this exception.

    Cheers and happy coding,
  3. Hi Karan,

    I guess the problem is with JNDI properties. When you use the same class with servlets/jsps, you will not find this problem becasue those run in container. When you try to test the DAO class from outside the container (from main()), you find this problem.

    The best way to deal with this is try to change the code to get the connection to make sure that it gives a normal connection(other than from Data Source) when this exception comes up.

    Another approach that I am not sure is set the JNDI properties before lookup the data source. Make sure that server is running while you test the DAO in this approach.

    Hope it helps !!!

    Anil Sadineni.
  4. You need t set the server properties so as to get the InitialContext. Which server are you using.

  5. I am also getting same exception when i am working with tomcat5.0.28. i am using jndi name from tomcat.
  6. hey I am getting thos error when I am using a datasource I am trying to setupa datasource and pool the db connections. help me
  7. EJB 3.0 Just SIX Steps to run EJB 3.0 EJB 3.0 stateless session bean EJB 3.0 sample application using jboss EJB 3.0 run and deploy using jboss Steps to built EJB 3.0 in Jboss Steps to run and deploy EJB 3.0 in jboss solution for javax.naming.NoInitialContextException: : Software requirenments: JDK : Download jdk from JBOSS: Download jboss server from Note: for this sample application I have used jboss-5.0.0.CR1 under c:\ ---further I will use the c:\jboss-5.0.0.CR1 path you can give your installation directory path instead of this Step 1 :Create EJB Sample Application Directory Structure: For easiness I am creating my sample application in c:\ you can create your project anywhere -My Sample Application's nameis MyEJB ---> create it as c:\MyEJB -Create two folder under- MyEJB folder-- > create it as c:\MyEJB\classes [this directory will contailn all the compiled class files i.e *.class file] and second folder---> create it as c:\MyEJB\src [this directory will contain all the sorce files i.e *.java files] -Create one folder named myapp under c:\MyEJB\classes----> crate it as c:\MyEJB\classes\myapp [this will be the package for class files like] -Create one folder named myapp under c:\MyEJB\src--> crate it as c:\MyEJB\src\myapp [this will be the package for java files like] Step 2 : Create java sorce files under c:\MyEJB\src\myapp directory [source code is given below] i ] package myapp; import javax.ejb.*; @Remote public interface EJBRemote { public String sayHi(String s); } ii ] package myapp; import javax.ejb.*; @Stateless public class EJBBean implements EJBRemote { public String sayHi(String name) { return "Good to be with you"+name; } } iii ] package myapp; import javax.ejb.*; import javax.naming.*; import java.util.Hashtable; public class Client { public static void main(String[] args) throws Exception { Hashtable env = new Hashtable(); env.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory"); env.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces"); //not required env.put("java.naming.provider.url","localhost:1099"); //not required Context ctx = new InitialContext(env); EJBRemote bean = (EJBRemote) ctx.lookup("EJBBean/remote"); //JBoss' default remote is /remote here EJBBean/remote String result = bean.sayHi(" Mukesh Lagadhir"); System.out.println(result); } } Note -After creating this three files your directory structure have to look like this c:\ +--->MyEJB +--->classes +myapp +--->src +myapp I------ I------ L____ Step 3 : Compile three files by this command in command window under command prompt go to sample application directory c:\MyEJB\classes > under this directory give command for compile java files this command uses c:\jboss-5.0.0.CR1\client\jbossall-client.jar file this is in my computer.Put your jboss path instead of this c:\MyEJB\classes > javac -d . -classpath c:\jboss-5.0.0.CR1\client\jbossall-client.jar;. ..\src\myapp\*.java Note:this command will create three class files under c:\MyEJB\classes folder Step 4: start JBOSS server by giving following command c:\MyEJB\classes > set JAVA_HOME="c:\Program Files\jre1.6.0_04" Note :do not set this path if you have this path already as a environment variable c:\MyEJB\classes > c:\jboss-5.0.0.CR1\bin\run.bat Note:this comman will start the server and take few minitus start new command prompt and go to c:\MyEJB\classes > Step 5 : Package and autodeploy the jar file c:\MyEJB\classes > jar cvf c:\jboss-5.0.0.CR1\server\default\deploy\myejb.jar myapp\EJBBean.class myapp\EJBRemote.class Note:this command will create myejb.jar file under c:\jboss-5.0.0.CR1\server\default\deploy\ this folder is use for automatically deploying the jar file Step 6 : Ok now no more steps : Run client by giving following command c:\MyEJB\classes >java -cp c:\jboss-5.0.0.CR1\lib\jboss-aop-jdk50.jar;c:\jboss-5.0.0.CR1\client\jbossall-client.jar;c:\jboss-5.0.0.CR1\server\default\lib\jbosssx.jar;. myapp.Client Have you got the result like Good to be with you Mukesh Lagadhir . if you want to undeploy jar file just delete it . del c:\jboss-5.0.0.CR1\default\deploy\foo-ejb.jar __________________________________________________________________________________________________________________________________________________________________________________________________________ Author : Mukesh Lagadhir Gujarat,India If you have any query on this artical or having problem with other java SE / java EE code write me on
  8. Getting same error[ Go to top ]

    I am also getting same error while creating connection pool using the tomcat 6 and sql server 2008 r2.

    below are the error details.

    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

    please help me for this.