Using Tomcat 5.5, JRE 1.6.07, Jdk 1.6. JNDI:
mySQL DATABASE.
I used eclipse to develop the application.
I think it is a classpath problem but as i am new to Java i havent a clue where to start digging. I am not quite sure if i am supposed to have a jndi.properties file either. Please help
I need the Application class to hold a list of the classes as it will be polling an update table for any chages to the database and updating the relevant class. In which case the web page will be updated also. I am hoping this architecture is correct as I am new to Java and it is not this that is causing the problem as they classes and database is accessable through the doget() call and not if i call Application.main() via eclipse or a bat file
Classes:
Database sits in my package com.database
public class DBConnection
{
public static Connection getDBConnection() throws SQLException
{
Connection conn = null;
try
{
InitialContext ctx = new InitialContext();
DataSource ds = ( DataSource ) ctx.lookup("java:comp/env/jdbc/mydb" );
try
{
conn = ds.getConnection();
}
catch( SQLException e )
{
System.out.println( "Open connection failure: " + e.getMessage() );
}
}
catch( NamingException nEx )
{
nEx.printStackTrace();
}
return conn;
}
Application sits in my package com.mainapp:
public static void main(String[] args) {
// TODO Auto-generated method stub
//this class will access the db and retreive all the information
//this class will poll the updates table for any changes in which case will repopulate the
//class
_urlpaths = new URLPaths();
_news = new LatestNews();
_announcement = new Announcement();
_slideImages = new SlideImages();
//some sort of timer that calls updates every 30secs
Updates update = new Updates();
update.startUpdatesPolling();
System.out.print("Test to reach");
// update.stopUpdatesPolling();
}
Each of the classes use the database:
public void populateList(){
//get the db connection
_UrlList.clear();
Connection co = null;
try{
[B]co = DBConnection.getDBConnection();[/B]
if(co!=null){
PreparedStatement ps = null;
ps = co.prepareStatement("SELECT * FROM mos_url;");
ps.executeQuery();
ResultSet rs = ps.getResultSet() ;
if(rs!=null){
while(rs.next()){
boolean active = rs.getBoolean("activeFlag");
if(active){
int id = rs.getInt("Id");
String ImagePath = rs.getString("displayImagePath");
String path = rs.getString("path");
String displayName = rs.getString("displayName");
int type = rs.getInt("type");
UrlLinkType urlType = UrlLinkType.web;
URLLink url = new URLLink(id, ImagePath, path, displayName, urlType);
_UrlList.add(url);
…
The PROBLEM
In Tomcat the contxt.xml and the web.xml have been modified correctly. My application actualy works if I call Application.main(null) from the
public class MainOptionsScreen extends HttpServlet{
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException
{
but if i run Eclipse and call the Application main method i get the following error.
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(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at com.database.DBConnection.getDBConnection(DBConnection.java:31)
at com.mainapp.URLPaths.populateList(URLPaths.java:80)
at com.mainapp.URLPaths.<init>(URLPaths.java:71)
at com.mainapp.Application.main(Application.java:31)
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(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at com.database.DBConnection.getDBConnection(DBConnection.java:31)
at com.mainapp.LatestNews.processSQL(LatestNews.java:64)
at com.mainapp.LatestNews.<init>(LatestNews.java:17)
at com.mainapp.Application.main(Application.java:32)
connection is nulljavax.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(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at com.database.DBConnection.getDBConnection(DBConnection.java:31)
at com.mainapp.Announcement.populate(Announcement.java:86)
at com.mainapp.Announcement.<init>(Announcement.java:17)
at com.mainapp.Application.main(Application.java:33)
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(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at com.database.DBConnection.getDBConnection(DBConnection.java:31)
at com.mainapp.SlideImages.populateList(SlideImages.java:90)
at com.mainapp.SlideImages.<init>(SlideImages.java:49)
at com.mainapp.Application.main(Application.java:34)
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(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at com.database.DBConnection.getDBConnection(DBConnection.java:31)
at com.mainapp.Application.main(Application.java:40)
Test to reachjavax.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(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at com.database.DBConnection.getDBConnection(DBConnection.java:31)
at com.mainapp.Updates.checkForUpdates(Updates.java:50)
at com.mainapp.Updates$ToDoTask.run(Updates.java:28)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)
Thanks in advance