• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

InitialContext problem... the rest of the question

 
Linda Andersson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I�m working in WSAD and are trying to do a lookup aginst a session bean from a java class in a webproject.

The code looks like this:

InitialContext ctx = new InitialContext();
Object obj = ctx.lookup("ejb/Db2Connect");
Db2ConnectHome home = (Db2ConnectHome) PortableRemoteObject.narrow(obj, Db2ConnectHome.class);
Db2Connect db2Connect = home.create();

Under References in the web deployment descriptor I have made an EJB reference to the session bean and called id "ejb/Db2Connect".

When I run the code I get the 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
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:652)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:255)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:292)
at javax.naming.InitialContext.lookup(InitialContext.java:359)
at com.ford.vcc.w2vcc.connection.db2.Db2Loader.fakeABod(Db2Loader.java:120)
at com.ford.vcc.w2vcc.connection.db2.Db2Loader.main(Db2Loader.java:53)
NamingException i Db2Loader 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

It seems to me like the problem is with the InitialContext does not get instanciated correctly, but I do not understad why. Can someone please give me a hint about what I have done wrong.

//Linda
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to place a jndi.properties file in your classpath.
 
Linda Andersson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I suppose to write the jndi.properties my self? How do I then no what information to fill the parameters with?

//Linda
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a sample jndi.properties file for WebSphere:



Try it out
 
Linda Andersson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not know if I added jndi.properties in the right place. I added it in the EAR file under the folder properties. On the server I added it to the classpath.
This did not do any good att all. I then added the properties when I created the InitialContext like this:

java.util.Properties properties = new java.util.Properties();
properties.put(Context.PROVIDER_URL, "iiop://localhost:2809");
properties.putContext.INITIAL_CONTEXT_FACTORY, "com.ibm.websphere.naming.WsnInitialContextFactory");
InitialContext ctx = new InitialContext(properties);

Then I got this error:

javax.naming.ConfigurationException: The property com.ibm.ws.naming.wsn.factory.initial is not set. The most likely cause is that the jar which contains the file com/ibm/websphere/naming/jndiprovider.properties cannot be found by the class loader.
at com.ibm.websphere.naming.WsnInitialContextFactory.init_implClassCtor(WsnInitialContextFactory.java:190)
at com.ibm.websphere.naming.WsnInitialContextFactory.getInitialContext(WsnInitialContextFactory.java:108)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:674)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:255)
at javax.naming.InitialContext.init(InitialContext.java:231)
at javax.naming.InitialContext.<init>(InitialContext.java:207)
at com.ford.vcc.w2vcc.connection.db2.Db2Loader.fakeABod(Db2Loader.java:91)NamingException i Db2Loader javax.naming.ConfigurationException: The property com.ibm.ws.naming.wsn.factory.initial is not set. The most likely cause is that the jar which contains the file com/ibm/websphere/naming/jndiprovider.properties cannot be found by the class loader.

at com.ford.vcc.w2vcc.connection.db2.Db2Loader.main(Db2Loader.java:53)


What should I have done differently?

//Linda
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
 
Linda Andersson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem solved!
Thank you very much for the help. Thanks to the new error messages I got I could find the solution.
The thing was that when I ran my web project java class I ran it as a java application. I should have run it as a J2EE client.
I then tried to call my java class from a jsp page and it worked!!!
The InitialContext found its environment and I could do my jndi lookup.
I could even remove the classpath to jndi.properties.

The "big problem" turned out to have a very easy solution.

//Linda
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic