This discussion is archived
8 Replies Latest reply: Jun 26, 2012 2:43 AM by gimbal2 RSS

javax.naming.NameNotFoundException: remaining name:env

801933 Newbie
Currently Being Moderated
in my application, I add ojdbc6.jar, wlfullclient-10-3.jar.
and in weblogic administration console > services > datasource, I define a Datasource with name
jdbc/abc

and then in the application, I access it using
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
Context ctx = new InitialContext(env);
DataSource ds = (DataSource) ctx.lookup("jdbc/abc");
this code run successfully without any error.

But if I try
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
Context ctx = new InitialContext(env);
ctx = (Context)ctx.lookup("java:comp/env");
Error:javax.naming.NameNotFoundException: remaining name: env

why would this happen (it can read the Datasource jndi but not java:comp/env)?
It is a application for testing, not web application, and I run by right click in eclipse, select Run As > Java Applicatioin.

If I really want to test a web application server side and in the server side there is some code to read the web.xml <env-entry> segment, what should I do?
Thanks.
  • 1. Re: javax.naming.NameNotFoundException: remaining name:env
    801933 Newbie
    Currently Being Moderated
    I try
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
    env.put(Context.PROVIDER_URL, "t3://localhost:7001");
    env.put(Context.SECURITY_PRINCIPL, "weblogic");
    env.put(Context.SECURITY_CREDENTIALS, "password"); // weblogic password for username "weblogic"
    Context ctx = new InitialContext(env);
    ctx = (Context)ctx.lookup("java:comp/env");
    But the same
    Error:javax.naming.NameNotFoundException: remaining name: env
    is thrown!!
  • 2. Re: javax.naming.NameNotFoundException: remaining name:env
    gimbal2 Guru
    Currently Being Moderated
    For any help I suggest you post your Weblogic question in the Weblogic forum.

    https://forums.oracle.com/forums/category.jspa?categoryID=193
  • 3. Re: javax.naming.NameNotFoundException: remaining name:env
    801933 Newbie
    Currently Being Moderated
    thanks for the reply, already post the same question in the link you provided.
    But I am quite new to jndi, and is the following correct?

    if create InitialContext from server, call new InitialContext(); // not need any parameter
    if create InitialContext from client, need call new InitialContext(Hashtable<?,?> environment) which is vendor implementation specific?
  • 4. Re: javax.naming.NameNotFoundException: remaining name:env
    gimbal2 Guru
    Currently Being Moderated
    You need the second call on a client because you need to tell JNDI where your server lives. Host, IP, etc. You don't do that on the server because... well the server already knows where it lives :) And yes the values are not only application server specific, they may change between the version of the application server as well. On top of that you need application server specific client jars on the classpath just to be able to connect.

    You don't need to specify properties in code by the way, you can also put a jndi.properties file in the root of the classpath (of your client) and put your settings there. Then you can simply use the InitialContext constructor without parameters in your client.
  • 5. Re: javax.naming.NameNotFoundException: remaining name:env
    801933 Newbie
    Currently Being Moderated
    if the server has a method which include new InitialContext() like

    public void read throws NamingException{

    Context ctx = (Context) new InitialContext();
    String str1 = ctx.lookup("key1");
    ............
    }

    how could I test this method in a client outside j2ee container?
    because in client, if I call read, it will use the first new InitialContext() method.
  • 6. Re: javax.naming.NameNotFoundException: remaining name:env
    EJP Guru
    Currently Being Moderated
    You can't. The java: namespace is provided by the container.
  • 7. Re: javax.naming.NameNotFoundException: remaining name:env
    801933 Newbie
    Currently Being Moderated
    EJP wrote:
    You can't. The java: namespace is provided by the container.
    thanks for the response, this give me a new direction.
    I found in [http://pic.dhe.ibm.com/infocenter/wasinfo/fep/index.jsp?topic=%2Fcom.ibm.websphere.nd.multiplatform.doc%2Finfo%2Fae%2Fae%2Frnam_example_prop3.html|http://pic.dhe.ibm.com/infocenter/wasinfo/fep/index.jsp?topic=%2Fcom.ibm.websphere.nd.multiplatform.doc%2Finfo%2Fae%2Fae%2Frnam_example_prop3.html]
    which mention
    >
    Applications that do not run in a container cannot use java: lookup names because it is the container which sets the java: namespace up for the application.
    [\quote]

    the website is for WebSphere, just want to know is this apply to all container?
  • 8. Re: javax.naming.NameNotFoundException: remaining name:env
    gimbal2 Guru
    Currently Being Moderated
    user10064903 wrote:
    the website is for WebSphere, just want to know is this apply to all container?
    You already have EJPs reply (which you must have read since you marked it as helpful) and you now have an URL that basically backs up what he states. Time to draw your own conclusions dude and if you so desperately need proof, experiment yourself.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points