java.lang.NoClassDefFoundError:无法初始化类me.gacl.util.JdbcUtils

时间:2017-04-13 04:55:40

标签: java spring maven web jdbc

我正在使用spring4 + maven + mysql编写一个web项目。我写了一个像这样的jdbcUtil:

public class JdbcUtils {
    private static String driver = null;
    private static String url = null;
    private static String username = null;
    private static String password = null;

    static{
        try{
            InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
            Properties prop = new Properties();
            prop.load(in);

            driver = prop.getProperty("driver");
            url = prop.getProperty("url");
            username = prop.getProperty("username");
            password = prop.getProperty("password");              
            Class.forName(driver);

        }catch (Exception e) {
            e.printStackTrace();
            throw new ExceptionInInitializerError(e);
        }
    }
    public static Connection getConnection() throws SQLException{
        return DriverManager.getConnection(url, username,password);
    }
    public static void release(Connection conn,Statement st,ResultSet rs){
        if(rs!=null){
            try{
                rs.close();
            }catch (Exception e) {
                e.printStackTrace();
            }
            rs = null;
        }
        if(st!=null){
            try{
                st.close();
            }catch (Exception e) {
                e.printStackTrace();
            }
        }

        if(conn!=null){
            try{
                conn.close();
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

和像这样的jdbc测试类:

public class JdbcCRUDByPreparedStatement {
     @Test
        public void insert(){
            Connection conn = null;
            PreparedStatement st = null;
            ResultSet rs = null;
            try{
                conn = JdbcUtils.getConnection();
                String sql = "insert into users(id,name,password,email,school,authority,technical,telenumber) values(?,?,?,?,?,?,?,?)";
                st = conn.prepareStatement(sql);
                st.setInt(1, 1);
                st.setString(2, "xiaoming");
                st.setString(3, "root");
                st.setString(4, "939712464@qq.com");
                st.setString(5, "nanjing");
                st.setInt(6, 0);
                st.setString(7, "javaweb");
                st.setInt(8, 123456);
                int num = st.executeUpdate();
                if(num>0){
                    System.out.println("insert success!");
                }

            }catch (Exception e) {
                e.printStackTrace();
            }finally{
                //SQL执行完成之后释放相关资源
                JdbcUtils.release(conn, st, rs);
            }
        }

        @Test
        public void delete(){
            Connection conn = null;
            PreparedStatement st = null;
            ResultSet rs = null;
            try{
                conn = JdbcUtils.getConnection();
                String sql = "delete from users where id=?";
                st = conn.prepareStatement(sql);
                st.setInt(1, 1);
                int num = st.executeUpdate();
                if(num>0){
                    System.out.println("delete success");
                }
            }catch (Exception e) {
                e.printStackTrace();
            }finally{
                JdbcUtils.release(conn, st, rs);
            }
        }

        @Test
        public void update(){
            Connection conn = null;
            PreparedStatement st = null;
            ResultSet rs = null;
            try{
                conn = JdbcUtils.getConnection();
                String sql = "update users set name=?,email=? where id=?";
                st = conn.prepareStatement(sql);
                st.setString(1, "gacl");
                st.setString(2, "gacl@sina.com");
                st.setInt(3, 2);
                int num = st.executeUpdate();
                if(num>0){
                    System.out.println("update success!");
                }
            }catch (Exception e) {
                e.printStackTrace();

            }finally{
                JdbcUtils.release(conn, st, rs);
            }
        }

        @Test
        public void find(){
            Connection conn = null;
            PreparedStatement st = null;
            ResultSet rs = null;
            try{
                conn = JdbcUtils.getConnection();
                String sql = "select * from users where id=?";
                st = conn.prepareStatement(sql);
                st.setInt(1, 1);
                rs = st.executeQuery();
                if(rs.next()){
                    System.out.println(rs.getString("name"));
                }
            }catch (Exception e) {

            }finally{
                JdbcUtils.release(conn, st, rs);
            }
        }
}

但是当我作为Junite测试运行时,它无法成功运行,failuer trace:

java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Properties.java:434)
    at java.util.Properties.load0(Properties.java:353)
    at java.util.Properties.load(Properties.java:341)
    at me.gacl.util.JdbcUtils.<clinit>(JdbcUtils.java:22)
    at me.gacl.jdbc.JdbcCRUDByPreparedStatement.delete(JdbcCRUDByPreparedStatement.java:54)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

任何人都知道如何修复它?

2 个答案:

答案 0 :(得分:0)

检查结果:

InputStream in = JdbcUtils.class.getClassLoader()
                          .getResourceAsStream("db.properties");

如果找不到资源db.properties,您将收到null,这会导致您的例外:

prop.load(in);

Class.getResourceAsStream()的Javadoc声明:

  

返回:   InputStream对象;如果找不到具有此名称的资源,则返回null

如果您的资源db.properties与您的班级JdbcUtils位于同一个套餐中,则"db.properties"是正确的参考。否则,如果资源db.properties位于其他位置,则必须使用前导/来定义路径,例如"/db.properties"表示类路径的根。

答案 1 :(得分:0)

NoClassDefFoundError意味着该类在编译时存在于类路径中,但在运行时的类路径中不存在。