java 类无法实例_无法实例化类:org.apache.naming.java.javaURLContextFactory

我正在研究Junit测试文件,它加载SQL文件并将其加载到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());

}

}

}

当我测试文件时,我遇到了这个问题:

无法实例化类:org.apache.naming.java.javaURLContextFactory

你能告诉我如何解决这个问题吗?你也发现这个Java代码有什么问题吗?

解决方法:

我通过将Apache Tomcat中的库添加到运行时测试库来修复此问题.

在Netbeans中:

Project Properties -> Libraries -> Run Tests

Add JAR/Folder

我需要的两个库是catalina.jar和tomcat-juli.jar.你的里程可能有所不同

我在Tomcat的安装目录下找到了它们.例如:

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

apache-tomcat-7.0.34/lib/catalina.jar

注意其中一个jar位于bin目录中,另一个位于lib目录中

这可能不是解决问题的最佳方法.注入DataSource的方法会更好.

标签:java,junit

来源: https://codeday.me/bug/20191006/1862114.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值