This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

NoInitialContextException EJB Client (Java main class)  RSS feed

 
Sam Gehouse
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using a Java main class as a client to test a simple stateless session EJB that will spit out hello world.

I get exception below where I try to look up for home interface:

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:674)
at com.labcorp.messaging.MyTest.main(MyTest.java:34)

I get exception below in line:
home = (SessHome)ic.lookup("java:comp/env/ejb/com/myComp/messaging/SessHome");

com.myComp.messaging.SessHome is my Home interface.


public class MyTest {

public static void main(String[] args) {
InitialContext ic = null;
try {
ic = new InitialContext();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

SessHome home = null;
try {
home = (SessHome)ic.lookup("java:comp/env/ejb/com/labcorp/messaging/SessHome");
} catch (NamingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}


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:674)
at com.labcorp.messaging.MyTest.main(MyTest.java:34)
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In most cases, when doing a standalone client, you'll need to use a non-default constructor of InitialContext so it knows what to do. Usually, when you do a simple "new InitialContext()" there's some file somewhere that the class will use to actually create the right objects for you. But in a standalone client, that's not the case, right?

Check out your JNDI server docs to find out what properties to put in the Map you pass to the InitialContext constructor.
 
Mahen Sen
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there!
i also got the same problem when i tried to access
jndi names of the JBOSS server. Finally how could i solved this is, simply changed the main program into a servlet and run. I think mostly it will work. I hope u r familiar with servlets jsps.
Regards.
Mahen
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!