0 Replies Latest reply on Jan 12, 2011 10:16 AM by baji shaik

    Unable to create the InitialContext object for JNDI service

    baji shaik Newbie



      I am new to JNDI, Just I want to use the "Connection Pool" which is created by JBOSS server but unfortunatly I am getting the following error:



      Exception in thread "main" 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


      NOTE: I am using Jboss studio2.0 so that I didn't add any external jar files

      My code is as follows:

      1. "mysql-ds.xml" file is as follows, I copied this file to "jboss-as\server\default\deploy"  folder











              <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml -->







      2. "jndi.properties" file is as follows, This file is available in my classpath


      ### JBossNS properties





      3."DataSourceTest.java" file as follows:

      package com.pojo;

      import java.sql.Connection;

      import java.sql.ResultSet;

      import java.sql.SQLException;

      import java.sql.Statement;


      import javax.naming.InitialContext;

      import javax.naming.NamingException;

      import javax.sql.DataSource;



      public class DataSourceTest {

      public static void main(String[] args) throws Exception {




      private static void testDataSource()

      throws NamingException, SQLException {

      final String sql = "select version()";

      InitialContext ic = new InitialContext();

      DataSource ds = (DataSource) ic.lookup("jdbc/mysql1");

      Connection con = null;

      Statement stmt = null;

      ResultSet rs = null;

      try {

      con = ds.getConnection();

      stmt = con.createStatement();

      rs = stmt.executeQuery(sql);

      while(rs.next()) {

          System.out.println("Query '" + sql + "' returned " + rs.getString(1));


      } finally {

      if(rs != null) rs.close();

      if(stmt != null) stmt.close();

      if(con != null) con.close();






      Thanks in advance!!!!!!!!!!!!!!!!!