James
James

Reputation: 1293

JNDI Lookup fails on Liberty Profile

I'm using Liberty Profile v8.5.5.5 (WebSphere Application Server 8.5.5.5/wlp-1.0.8.cl50520150221-0034) on IBM J9 VM, version pxa6470sr1-20120330_01 (SR1) (en_US)

I have the jndi feature installed...but no matter what I do, I can't do a simple JNDI lookup.

In my server.xml

<jndiEntry jndiName="schoolOfAthens/defaultAdminUserName" value="plato" />

My code... (Which is just a servlet of a few lines)

Object jndiConstant = new InitialContext().lookup(
"schoolOfAthens/defaultAdminUserName");

But this fails with:

javax.naming.NameNotFoundException: Name schoolOfAthens not found in context "serverlocal:CELLROOT/SERVERROOT".

The code is taken directly from an example.

Any ideas?

I am running this locally and have also tried on my Bluemix account... Same result

Upvotes: 4

Views: 6810

Answers (2)

Gas
Gas

Reputation: 18050

Same works for me on 8.5.5.6, don't have .5 at hand but should work in the same way.

Here is my my server.xml:

<server description="new server">

    <!-- Enable features -->
    <featureManager>
        <feature>servlet-3.1</feature>
        <feature>jndi-1.0</feature>
        <feature>localConnector-1.0</feature>
    </featureManager>

    <!-- To access this server from a remote client add a host attribute to the following element, e.g. host="*" -->
    <httpEndpoint httpPort="9080" httpsPort="9443" id="defaultHttpEndpoint"/>


    <applicationMonitor updateTrigger="mbean"/>

    <webApplication id="JNDITest" location="JNDITest.war" name="JNDITest"/>
    <jndiEntry jndiName="schoolOfAthens/defaultAdminUserName" value="plato" />    
</server>

And servlet code (take a look that you can also use @Resource annotation instead of lookup):

@WebServlet("/JNDIServlet")
public class JNDIServlet extends HttpServlet {
    @Resource(lookup="schoolOfAthens/defaultAdminUserName")
    String jndiVariable;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            InitialContext ctx = new InitialContext();
            Object object = ctx.lookup("schoolOfAthens/defaultAdminUserName");
            System.out.println("object: " + object);
            System.out.println("jndiVariable: " + jndiVariable);
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }

}

with output:

object: plato
jndiVariable: plato

Upvotes: 0

James
James

Reputation: 1293

ok , got this to work. I added a resource-ref to my web.xml and looked it up like this:

 Object obj2 = ctx.lookup("java:comp/env/schoolOfAthens/defaultAdminUserName");`

web.xml

 <resource-ref>
     <description>Test Reference</description>
     <res-ref-name>schoolOfAthens/defaultAdminUserName</res-ref-name>
     <res-auth>Container</res-auth>
 </resource-ref>

Upvotes: 2

Related Questions