| Author |
Context Lookup Failing
|
A Shar
Greenhorn
Joined: Nov 16, 2003
Posts: 3
|
|
I am working on a pretty simple program using JBuilder and WebLogic. I have depolyed a simple cart JAR to WebLogic 8.1. Then wrote a test client to test the EJB. The JAR has deployed fine, and I can test the Cart JNDI name successfuly on WebLogic. But when I run the test client program it fails when it tries to do a context lookup on the Cart. Object ref = context.lookup("Cart"); The Cart is actively deployed. Any suggestion on how to fix this problem? Test Client code snippet: ===================================== public void initialize() { try { //get naming context System.out.println("I am here1\n" ); Context context = new InitialContext(); if (context != null) { System.out.println("I am here2\n" ); } //look up jndi name Object ref = context.lookup("Cart"); if (ref == null) { System.out.println("I am here3\n" ); } //look up jndi name and cast to Home interface cartHome = (CartHome) PortableRemoteObject.narrow(ref, CartHome.class); } catch(Exception e) { e.printStackTrace(); } } End Code Snippet========================================================= Exception Thread: 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(NamingManager.java:640) at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:243) at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:280) at javax.naming.InitialContext.lookup(InitialContext.java:347) at cart_session.CartTestClient1.initialize(CartTestClient1.java:25) at cart_session.CartTestClient1.<init>(CartTestClient1.java:13) at cart_session.CartTestClient1.main(CartTestClient1.java:48) Thanks for your help!
|
 |
Lasse Koskela
author
Sheriff
Joined: Jan 23, 2002
Posts: 11962
|
|
|
The problem is that outside of the container, you need to configure the InitialContext instead of just saying new InitialContext(). Take a look at this for the correct configuration for WLS8.1.
|
Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
|
 |
A Shar
Greenhorn
Joined: Nov 16, 2003
Posts: 3
|
|
Lasse, Thanks for your reply. I tried to set up the context using the code from the link you provided. Still, it isn't working. I do get a ontext, but the code fails when it tries to do a lookup on cart. The error is the same. Here is the new code: New Code======================== package cart_session; import javax.naming.*; import javax.rmi.PortableRemoteObject; import java.util.*; public class CartTestClient1 extends Object { private CartHome cartHome = null; //Construct the EJB test client public CartTestClient1() { System.out.println("I am here\n" ); initialize(); } public void initialize() { try { //get naming context System.out.println("I am here1\n" ); Context ctx = null; Hashtable ht = new Hashtable(); ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); ht.put(Context.PROVIDER_URL, "t3://localhost:7001"); 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 } } //Context context = new InitialContext(); if (ctx != null) { System.out.println("I am here2\n" ); } //look up jndi name Object ref = ctx.lookup("Cart"); //Code fails here// if (ref == null) { System.out.println("I am here3\n" ); } End new code ==================================================== Thanks for your help!
|
 |
Lasse Koskela
author
Sheriff
Joined: Jan 23, 2002
Posts: 11962
|
|
Have you used a real JNDI name or an ejb-ref? It would help if you'd post the ejb-jar.xml and weblogic-ejb-jar.xml deployment descriptors (only those parts relevant to the "Cart" bean, please). By the way, "Electron Proton" fails the obviously-fake test of our naming policy so you'll need to change that into something real-looking.
|
 |
A Shar
Greenhorn
Joined: Nov 16, 2003
Posts: 3
|
|
Here is the weblogic-ejb-jar.xml ====Begin weblogic-ejb-jar.xml ==== <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE weblogic-ejb-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 8.1.0 EJB//EN' 'http://www.bea.com/servers/wls810/dtd/weblogic-ejb-jar.dtd'> <weblogic-ejb-jar> <weblogic-enterprise-bean> <ejb-name>Cart</ejb-name> <jndi-name>Cart</jndi-name> </weblogic-enterprise-bean> </weblogic-ejb-jar> ====End weblogic-ejb-jar.xml==== Here is the ejb-jar.xml === Begin ejb-jar.xml ====== <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd"> <ejb-jar> <enterprise-beans> <session> <display-name>Cart</display-name> <ejb-name>Cart</ejb-name> <home>cart_session.CartHome</home> <remote>cart_session.Cart</remote> <ejb-class>cart_session.CartBean</ejb-class> <session-type>Stateful</session-type> <transaction-type>Container</transaction-type> </session> </enterprise-beans> <assembly-descriptor> <container-transaction> <method> <ejb-name>Cart</ejb-name> <method-name>*</method-name> </method> <trans-attribute>Required</trans-attribute> </container-transaction> </assembly-descriptor> </ejb-jar> === End ejb-jar.xml============== I converted the context object to string and this is what I get: javax.naming.InitialContext@19b04e2 -- Eventhough the exception says 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 ==================================== Thanks for your help
|
 |
 |
|
|
subject: Context Lookup Failing
|
|
|