javax.naming.NoInitialContextException problem

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 1
Reputation: PCheema is an unknown quantity at this point 
Solved Threads: 0
PCheema PCheema is offline Offline
Newbie Poster

New to java need help with javax.naming.NoInitialContextException problem

 
0
  #1
Oct 17th, 2008
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

  1. public class DBConnection
  2. {
  3. public static Connection getDBConnection() throws SQLException
  4. {
  5. Connection conn = null;
  6.  
  7. try
  8. {
  9. InitialContext ctx = new InitialContext();
  10. DataSource ds = ( DataSource ) ctx.lookup("java:comp/env/jdbc/mydb" );
  11.  
  12. try
  13. {
  14. conn = ds.getConnection();
  15. }
  16. catch( SQLException e )
  17. {
  18. System.out.println( "Open connection failure: " + e.getMessage() );
  19. }
  20. }
  21. catch( NamingException nEx )
  22. {
  23. nEx.printStackTrace();
  24. }
  25. return conn;
  26. }

Application sits in my package com.mainapp:
  1. public static void main(String[] args) {
  2. // TODO Auto-generated method stub
  3.  
  4. //this class will access the db and retreive all the information
  5. //this class will poll the updates table for any changes in which case will repopulate the
  6. //class
  7. _urlpaths = new URLPaths();
  8. _news = new LatestNews();
  9. _announcement = new Announcement();
  10. _slideImages = new SlideImages();
  11. //some sort of timer that calls updates every 30secs
  12. Updates update = new Updates();
  13. update.startUpdatesPolling();
  14.  
  15. System.out.print("Test to reach");
  16. // update.stopUpdatesPolling();
  17.  
  18. }

Each of the classes use the database:
  1. public void populateList(){
  2. //get the db connection
  3. _UrlList.clear();
  4. Connection co = null;
  5. try{
  6. [B]co = DBConnection.getDBConnection();[/B]
  7. if(co!=null){
  8.  
  9. PreparedStatement ps = null;
  10. ps = co.prepareStatement("SELECT * FROM mos_url;");
  11. ps.executeQuery();
  12. ResultSet rs = ps.getResultSet() ;
  13. if(rs!=null){
  14. while(rs.next()){
  15. boolean active = rs.getBoolean("activeFlag");
  16. if(active){
  17. int id = rs.getInt("Id");
  18. String ImagePath = rs.getString("displayImagePath");
  19. String path = rs.getString("path");
  20. String displayName = rs.getString("displayName");
  21. int type = rs.getInt("type");
  22. UrlLinkType urlType = UrlLinkType.web;
  23. URLLink url = new URLLink(id, ImagePath, path, displayName, urlType);
  24. _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
  1. public class MainOptionsScreen extends HttpServlet{
  2. public void doGet(HttpServletRequest request,
  3. HttpServletResponse response) throws IOException
  4. {

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
Last edited by cscgal; Oct 17th, 2008 at 11:38 am. Reason: readability / Added code tags
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC