GeeCON Prague 2014*
The moose likes IDEs, Version Control and other tools and the fly likes Please Help with Error!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "Please Help with Error!!" Watch "Please Help with Error!!" New topic
Author

Please Help with Error!!

Elle Atechsy
Ranch Hand

Joined: Jan 23, 2004
Posts: 96
Hi,



I'm very new to Java and WSAD 5.1. I have a database connection method that I can call from any other class "named method" with NO a problem. However, whenever I try to call the same database connection method from a "main" method within other classes, I get the following error.

********ERROR Starts********

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.ubs.mcs.oos.dao.RDBMSBaseDAO.getDatabaseConnection(RDBMSBaseDAO.java:51)
at com.ubs.mcs.oos.testClasses.testClassCall.main(testClassCall.java:30)
Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initialSomething is wrong!!!

at com.ubs.mcs.oos.dao.RDBMSBaseDAO.getDatabaseConnection(RDBMSBaseDAO.java:60)
at com.ubs.mcs.oos.testClasses.testClassCall.main(testClassCall.java:30)

********ERROR ENDS********

Here is what my database connection method looks like:

********DB Connection Method Starts********
public synchronized Connection getDatabaseConnection(String appName)
throws DAOException {

String pool = appName + "Pool";
try {
javax.sql.DataSource ds = (javax.sql.DataSource) getInitialContext().lookup(pool);
Connection conn = ds.getConnection();
return conn;
} catch (Exception e){
e.printStackTrace();
throw new DAOException(e);
}
}
********DB Connection Method ENDS********

********Call to DB Connection Method from w/i Main method of another class.

conn = getElems.getDatabaseConnection(mesRes.getMessage("appID.acronym"));



I appreciate any help provided.

Thanks,
Lulu
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30585
    
154

Lulu,
When you run code from a main method, you are outside the server's JVM. This code would work if run from an EJB or servlet.

If you are running from an application client, WSAD will recognize the initial context. If not, you have to pass remote parameters. Search this, the J2EE and WebSphere forums for some examples.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Elle Atechsy
Ranch Hand

Joined: Jan 23, 2004
Posts: 96
Since it's just a test, I tried it in a servlet. It worked. Thanks for the explaination!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please Help with Error!!