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;
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
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
.