简体   繁体   English

尽管添加了依赖项,但java.lang.ClassNotFoundException

[英]java.lang.ClassNotFoundException inspite of adding dependency

My connection manager class is as follows: 我的连接管理器类如下:

package com.ideas.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionManager {

    public Connection getConnection() {
        Connection connection = null ; 


        //next two lines are always same
        try {

            Class.forName("com.mysql.cj.jdbc.Driver");

            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/formsdb", "root", "root");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;

    }
}

and i have written following test for the above class: 我已经为上述课程写了以下测试:

package com.ideas.db;

import org.junit.Assert;
import org.junit.Test;

public class ConnectionManagerTest {
    @Test
    public void shouldGetConnection(){
        ConnectionManager connectionManager= new ConnectionManager();
        Assert.assertNotNull(connectionManager.getConnection());
    }
}

I have added following dependency in my pom.xml file: 我在pom.xml文件中添加了以下依赖项:

<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>6.0.3</version>
    </dependency>

However, I am still getting the error when I run the test 但是,运行测试时我仍然收到错误消息

java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:370)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at com.ideas.db.ConnectionManager.getConnection(ConnectionManager.java:16)
    at com.ideas.db.ConnectionManagerTest.shouldGetConnection(ConnectionManagerTest.java:10)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    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:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.util.zip.ZipException: invalid LOC header (bad signature)
    at java.util.zip.ZipFile.read(Native Method)
    at java.util.zip.ZipFile.access$1400(ZipFile.java:61)
    at java.util.zip.ZipFile$ZipFileInputStream.read(ZipFile.java:717)
    at java.util.zip.ZipFile$ZipFileInflaterInputStream.fill(ZipFile.java:420)
    at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
    at java.util.jar.Manifest$FastInputStream.fill(Manifest.java:441)
    at java.util.jar.Manifest$FastInputStream.readLine(Manifest.java:375)
    at java.util.jar.Manifest$FastInputStream.readLine(Manifest.java:409)
    at java.util.jar.Attributes.read(Attributes.java:376)
    at java.util.jar.Manifest.read(Manifest.java:199)
    at java.util.jar.Manifest.<init>(Manifest.java:69)
    at java.util.jar.JarFile.getManifestFromReference(JarFile.java:199)
    at java.util.jar.JarFile.getManifest(JarFile.java:180)
    at sun.misc.URLClassPath$JarLoader$2.getManifest(URLClassPath.java:944)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:450)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
    ... 33 more

Looks like the MySQL jar file may corrupted: 看起来MySQL jar文件可能已损坏:

Caused by: java.util.zip.ZipException: invalid LOC header (bad signature)

Delete the JAR file from your local Maven repository and try running Maven again. 从本地Maven存储库中删除JAR文件,然后尝试再次运行Maven。

Your ZIP file seems defective. 您的ZIP文件似乎有缺陷。

JAR files are essentially ZIP files. JAR文件本质上是ZIP文件。 Remove the jar file from the local repository and reload it from the dependency server. 从本地存储库中删除jar文件,然后从依赖项服务器重新加载它。 You can also test the jar file using the jar application. 您还可以使用jar应用程序测试jar文件。

The essential exception is 重要的例外是

Caused by: java.util.zip.ZipException: invalid LOC header (bad signature)
    at java.util.zip.ZipFile.read(Native Method)
    at java.util.zip.ZipFile.access$1400(ZipFile.java:61)

This means, your downloaded dependency cannot be properly unpacked/loaded. 这意味着您下载的依赖项无法正确解压/加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM