I have problem in accessing JNDI?


General J2EE: I have problem in accessing JNDI?

  1. I have problem in accessing JNDI? (4 messages)

    Here is the problem :

    I am using a class which invokes a bean using jndi
    java://comp/env/ejb/account/Account , when I am using this within bean container then this gets invoked , the code
    is written in x class which is getting called when bean is invoked .

    public class x {

    public static void getJndi() {
    InitialContext inc=new InitialContext();
    String classname=inc.lookup("java://comp/env/ejb/account/Account");


    this gets invoked when x.getJndi() is called within bean , bean can access because , for InitialContext - If you use the class to retrieve the EJB in the same container, you can simply pass null or ignore them.
    like InitialContext inc=new InitialContext();

    But how I invoke this getJndi() method without using ejb container,as specified in java documentation if "All parameters are the environment values for creating the initial context, which include the initial context factory, provider URL, user ID, and password then context can be invoked even from simple java code" , but In my case I don't have IntialContext with parameters ? so please could anybody gives me solution , by which I can invoke getJndi even from normal class.
  2. I have problem in accessing JNDI?[ Go to top ]

    This is an example for WebLogic, your own server should give something like this in its documentation.
      Context ctx = null;
      Hashtable ht = new Hashtable();

      try {
        ctx = new InitialContext(ht);
        // Use the context in your program
      catch (NamingException e) {
        // a failure occurred
      finally {
        try {ctx.close();}
        catch (Exception e) {
          // a failure occurred
  3. I have problem in accessing JNDI?[ Go to top ]

    Thanks Saruman,

    But this will not solve my purpose , I am invoking class x
    through reflection , so I don't have authority to make changes in class x , so I can't use IntialContext with parameters .
    I need solution which allows me to do reflection on class x which has InitialContext without parameters.

    .. Thanks In advance
  4. I have problem in accessing JNDI?[ Go to top ]

    Look into using a jndi.properties file.
  5. I have problem in accessing JNDI?[ Go to top ]

    Thanks Matt.

    Thanks for suggestions - I tried and got following error

    Error on Execution

    TESTING.................. Inside doGet method
    Error: 500
    Location: /test/servlet/testingAccount
    Internal Servlet Error:

    com.snstech.account.exceptions.AccountException: AccountX.getDAO: NamingException while getting DAO type :
    env not bound
    at com.snstech.account.dao.AccountX.getDAO(AccountX.java:32)
    at testingAccount.doGet(testingAccount.java:24)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
    at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:405)
    at org.apache.tomcat.core.Handler.service(Handler.java:287)
    at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)
    at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:812)
    at org.apache.tomcat.core.ContextManager.service(ContextManager.java:758)
    at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:213)
    at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)
    at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:501)
    at java.lang.Thread.run(Thread.java:484)

    Execution Path

    1) Created testingAccount.java , to test and not using reflection.
    2) Set classpath for jndi.properties in run.bat.


    import javax.naming.NamingException;
    import javax.naming.InitialContext;
    import com.snstech.account.dao.*;

    import com.snstech.account.exceptions.AccountException;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Enumeration;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.naming.*;
    import java.util.*;

    public class testingAccount extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    PrintWriter out = response.getWriter();
    out.println("\nInside doGet method");
    System.out.println("\nInside doGet method");



    package com.snstech.account.dao;

    import javax.naming.NamingException;
    import javax.naming.InitialContext;

    import com.sun.j2ee.blueprints.customer.util.JNDINames;
    import com.snstech.account.exceptions.AccountException;

    public class AccountX {
    public static Account getDAO() throws AccountException {
    String static final JNDI_NAME="java:comp/env/ejb/account/Account";
    Account acctDao = null;
    try {
    InitialContext ic = new InitialContext();
    String className = (String) ic.lookup(JNDI_NAME);
    acctDao = (Account) Class.forName(className).newInstance();
    } catch (NamingException ne) {
    throw new AccountException("AccountX.getDAO: NamingException while getting DAO type : \n" + ne.getMessage());
    } catch (Exception se) {
    throw new AccountException("AccountX.getDAO: Exception while getting DAO type : \n" + se.getMessage());
    return acctDao;

    setting jndi properties

    set classpath=%classpath%;D:\JBoss\jboss\admin\client\jndi.properties

    Please help!!!

    -- Thanks In Advance