Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot instantiate class: org.apache.naming.java.javaURLContextFactory

Tags:

java

junit

I'm working on Junit test file which loads SQL file and loads it into Oracle:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import oracle.jdbc.pool.OracleConnectionPoolDataSource;
import org.junit.BeforeClass;
import org.junit.Test;

public class OracleCreateScheme1
{

    public OracleCreateScheme1()
    {
    }

    @BeforeClass
    public static void setUpClass() throws Exception
    {
        // rcarver - setup the jndi context and the datasource
        try
        {
            // Create initial context
            System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                    "org.apache.naming.java.javaURLContextFactory");
            System.setProperty(Context.URL_PKG_PREFIXES,
                    "org.apache.naming");
            InitialContext ic = new InitialContext();

            ic.createSubcontext("java:");
            ic.createSubcontext("java:/comp");
            ic.createSubcontext("java:/comp/env");
            ic.createSubcontext("java:/comp/env/jdbc");

            // Construct DataSource
            OracleConnectionPoolDataSource ds = new OracleConnectionPoolDataSource();
            ds.setURL("jdbc:oracle:thin:@192.168.1.104:1521:oracle");
            ds.setUser("admin");
            ds.setPassword("qwerty");

            ic.bind("java:/comp/env/jdbc/oracle", ds);
        }
        catch (NamingException ex)
        {
            //Logger.getLogger(MyDAOTest.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    @Test
    public void createOracleScheme() throws SQLException, NamingException
    {

        Context initContext = new InitialContext();
        Context webContext = (Context) initContext.lookup("java:/comp/env");

        DataSource ds = (DataSource) webContext.lookup("jdbc/Oracle");

        // Read File ------------------------------------------------------------------

        String s = new String();
        StringBuffer sb = new StringBuffer();

        try
        {
            FileReader fr = new FileReader(new File("OracleScheme.sql"));

            BufferedReader br = new BufferedReader(fr);

            while ((s = br.readLine()) != null)
            {
                sb.append(s);
            }
            br.close();

            // here is our splitter ! We use ";" as a delimiter for each request
            // then we are sure to have well formed statements
            String[] inst = sb.toString().split(";");

            Connection c = ds.getConnection();
            Statement st = c.createStatement();

            for (int i = 0; i < inst.length; i++)
            {
                // we ensure that there is no spaces before or after the request string
                // in order to not execute empty statements
                if (!inst[i].trim().equals(""))
                {
                    st.executeUpdate(inst[i]);
                    System.out.println(">>" + inst[i]);
                }
            }

        }
        catch (Exception e)
        {
            System.out.println("*** Error : " + e.toString());
            System.out.println("*** ");
            System.out.println("*** Error : ");
            e.printStackTrace();
            System.out.println("################################################");
            System.out.println(sb.toString());
        }

    }
}

When I test the file I get this problem:

Cannot instantiate class: org.apache.naming.java.javaURLContextFactory

Can you tell me how I can solve this problem? And also do you find any problem into this Java code?

like image 730
Peter Penzov Avatar asked Feb 19 '13 16:02

Peter Penzov


2 Answers

I fixed this by adding libraries from Apache Tomcat to the Run-time Test Libraries.

In Netbeans:

Project Properties -> Libraries -> Run Tests

Add JAR/Folder

The two libraries I needed were catalina.jar and tomcat-juli.jar. You mileage may vary.

I found them under the installation directory for Tomcat. e.g:

apache-tomcat-7.0.34/bin/tomcat-juli.jar
apache-tomcat-7.0.34/lib/catalina.jar

Note one of the jars is in the bin directory, the other in the lib directory

This probably isn't the best way to fix your problem. It would be better to have a different way to inject the DataSource.

like image 105
Peter Wood Avatar answered Nov 15 '22 13:11

Peter Wood


I was getting this error as the jar that has this class was not added to my classpath. Exception was

Exception in thread "main" javax.naming.NoInitialContextException: Cannot instantiate
class: org.apache.naming.java.javaURLContextFactory [Root exception is java.lang.Class
NotFoundException: org.apache.naming.java.javaURLContextFactory]
        at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:657)
        at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
        at javax.naming.InitialContext.init(InitialContext.java:223)
        at javax.naming.InitialContext.<init>(InitialContext.java:175)
        at ContextLoaderTest.setDataSourceInformation(ContextLoaderTest.java:51)
        at ContextLoaderTest.main(ContextLoaderTest.java:34)
Caused by: java.lang.ClassNotFoundException: org.apache.naming.java.javaURLContextFact
ory

You need to add the catalina jar file. For that add following in your Maven dependencies -

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>catalina</artifactId>
    <version>6.0.43</version>
</dependency>

Note artifact name has changed now. Artifact name is tomcat-catalina instead of 'catalina '

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-catalina</artifactId>
    <version>8.0.15</version>
</dependency>

. So use latest version from

http://mvnrepository.com/artifact/org.apache.tomcat/tomcat-catalina

than

http://mvnrepository.com/artifact/org.apache.tomcat/catalina

like image 41
Aniket Thakur Avatar answered Nov 15 '22 12:11

Aniket Thakur