6

I've been trying to find a solution here but I cant... I have the following code and i get this error.

Am I missing something? Thank you :)

Code

package src;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Wrapper;
import java.util.Hashtable;
import java.util.Properties;
import java.io.*;
import javax.*;
import javax.activation.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import oracle.jdbc.pool.OracleDataSource;

public class TestServlet {

    @SuppressWarnings("unused")
    public static void main(String[] argv) throws SQLException, NamingException {

        Context initialContext = new InitialContext();
        if ( initialContext == null){System.out.println("initialContext null");}
        else {System.out.println("initialContext");}

        // Get DataSource
        Context environmentContext = (Context)initialContext.lookup("java:/comp/env");
        if ( environmentContext == null){System.out.println("envContext null.");}
        else {System.out.println("envContext");}

        DataSource ds = (DataSource)environmentContext.lookup("jdbc/testdb");



        System.out.println("\n -------- Oracle JDBC Connection Testing ------");

        try {

            Connection jdbcConnection = ((Statement) ds).getConnection();
            OracleDataSource ods = ((Wrapper) ds).unwrap(OracleDataSource.class);
            jdbcConnection.close();

        } catch (SQLException e) {

            System.out.println("Connection Failed! Check output console");
            e.printStackTrace();
            return;

        }

        String message = "You are connected!";
        System.out.println(message);

    }

}

context.xml

<Context>
    <Resource name="jdbc/testdb"
    auth="Container"
    type="javax.sql.DataSource" 
    maxActive="100"
    maxIdle="30"
    maxWait="10000" 
    username="dba01"
    password="qvE-g7Cacontext.xml"
    driverClassName="com.mysql.jdbc.Driver" 
    url="jdbc:oracle:thin:@10.0.1.6:1521:xe"/>
</Context>

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
    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 src.TestServlet.main(TestServlet.java:34)

Please let me know if you need more information!

user01230
  • 563
  • 1
  • 6
  • 16
  • Maybe this answer could help: http://stackoverflow.com/questions/21242733/tomcat-embedded-noinitialcontextexception-when-trying-to-get-a-datasource – morgano Mar 11 '15 at 19:23

3 Answers3

4

You need an initial context factory. For Tomcat it is org.apache.naming.java.javaURLContextFactory:

        System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                  "org.apache.naming.java.javaURLContextFactory");
Necreaux
  • 9,451
  • 7
  • 26
  • 43
  • 1
    You also need to run it inside Tomcat as Zielu mentioned. That's also a big one. – Necreaux Mar 11 '15 at 19:31
  • I managed to do this! But now I have the following error.. Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory – user01230 Mar 11 '15 at 19:42
  • @user01230 - you need to download tomcat-juli.jar and add it to your build path (classpath) – BustedSanta Jul 07 '16 at 12:43
2

So is it the code deployed on the server? It seems you try to access the Context using standard java as you call static main (so I guess you are calling TeestServlet.main from your runtime). The context will be setup by the web server (Tomcat), so it is availble only after you deploy your web application to it.

Zielu
  • 8,312
  • 4
  • 28
  • 41
  • Hi Zielu, it is not deployed on the server! Is it possible to make it work in eclipse? – user01230 Mar 11 '15 at 19:30
  • Are you actually trying to develop a servlet or do you simply want to have a DataSource and you used that code to get one? – Zielu Mar 11 '15 at 19:33
  • I am trying to develop a servlet. But first I do all of my testing using a DataSource and them I would like to create a servlet to run it using command line~ – user01230 Mar 11 '15 at 19:34
  • You cannot run serlvet using command line. You need servlet container == tomcat, or you need to embed the container inside your application. – Zielu Mar 11 '15 at 19:38
  • You seem to be new, so embedding container may be tricky for you. You can consider using SpringBoot to start you project as it will create for your the app that you can run from command line. – Zielu Mar 11 '15 at 19:40
  • I was able to run the tomcat server using eclipse! but now I am getting this error: Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory. I added juli on the path but still does not wok.. Any ideas? Thank you – user01230 Mar 11 '15 at 20:02
0

Please note that driverClassName that you are using is incorrect. It should be "oracle.jdbc.OracleDriver" if you are trying to connect to Oracle Database.

<Context>
    <Resource name="jdbc/testdb"
    auth="Container"
    type="javax.sql.DataSource" 
    maxActive="100"
    maxIdle="30"
    maxWait="10000" 
    username="dba01"
    password="qvE-g7Cacontext.xml"
    driverClassName="com.mysql.jdbc.Driver" 
    url="jdbc:oracle:thin:@10.0.1.6:1521:xe"/>
</Context>
Nirmala
  • 1,278
  • 1
  • 10
  • 11