Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.lang.ClassNotFoundException,尽管添加了依赖项_Java_Mysql_Maven - Fatal编程技术网

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

java.lang.ClassNotFoundException,尽管添加了依赖项,java,mysql,maven,Java,Mysql,Maven,我的连接管理器类如下所示: 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 ;

我的连接管理器类如下所示:

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;

    }
}
我为上述课程写了以下测试:

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());
    }
}
我在pom.xml文件中添加了以下依赖项:

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

mysql
mysql连接器java
6.0.3
但是,当我运行测试时,仍然会出现错误

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
java.lang.ClassNotFoundException:com.mysql.cj.jdbc.Driver
位于java.net.URLClassLoader$1.run(URLClassLoader.java:370)
在java.net.URLClassLoader$1.run(URLClassLoader.java:362)
位于java.security.AccessController.doPrivileged(本机方法)
位于java.net.URLClassLoader.findClass(URLClassLoader.java:361)
位于java.lang.ClassLoader.loadClass(ClassLoader.java:424)
位于sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
位于java.lang.ClassLoader.loadClass(ClassLoader.java:357)
位于java.lang.Class.forName0(本机方法)
位于java.lang.Class.forName(Class.java:264)
在com.ideas.db.ConnectionManager.getConnection上(ConnectionManager.java:16)
位于com.ideas.db.ConnectionManagerTest.shouldGetConnection(ConnectionManagerTest.java:10)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:497)
位于org.junit.runners.model.FrameworkMethod$1.runReflectVeCall(FrameworkMethod.java:50)
位于org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
位于org.junit.runners.model.FrameworkMethod.invokeeexplosive(FrameworkMethod.java:47)
位于org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
位于org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
位于org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
位于org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
位于org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
位于org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
位于org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
访问org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
位于org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
位于org.junit.runners.ParentRunner.run(ParentRunner.java:363)
位于org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
位于org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
原因:java.util.zip.ZipException:无效的LOC头(错误签名)
位于java.util.zip.ZipFile.read(本机方法)
access$1400(ZipFile.java:61)
位于java.util.zip.ZipFile$ZipFileInputStream.read(ZipFile.java:717)
位于java.util.zip.ZipFile$zipfileInflatierInputStream.fill(ZipFile.java:420)
位于java.util.zip.InflateInputStream.read(InflateInputStream.java:158)
位于java.util.jar.Manifest$FastInputStream.fill(Manifest.java:441)
位于java.util.jar.Manifest$FastInputStream.readLine(Manifest.java:375)
位于java.util.jar.Manifest$FastInputStream.readLine(Manifest.java:409)
位于java.util.jar.Attributes.read(Attributes.java:376)
位于java.util.jar.Manifest.read(Manifest.java:199)
位于java.util.jar.Manifest。(Manifest.java:69)
位于java.util.jar.JarFile.getManifestFromReference(JarFile.java:199)
位于java.util.jar.JarFile.getManifest(JarFile.java:180)
位于sun.misc.URLClassPath$JarLoader$2.getManifest(URLClassPath.java:944)
位于java.net.URLClassLoader.defineClass(URLClassLoader.java:450)
在java.net.URLClassLoader.access$100(URLClassLoader.java:73)
在java.net.URLClassLoader$1.run(URLClassLoader.java:368)
... 33多

您的ZIP文件似乎有缺陷

JAR文件本质上是ZIP文件。从本地存储库中删除jar文件,并从依赖服务器重新加载它。您还可以使用jar应用程序测试jar文件

基本例外是

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)

这意味着您下载的依赖项无法正确解压缩/加载

看起来MySQL jar文件可能已损坏:

原因:java.util.zip.ZipException:无效的LOC头(错误签名)


从本地Maven存储库中删除JAR文件,然后再次尝试运行Maven。

@Abihabi87否。请参见此处:理论上,您甚至不需要
Class.forName()
,因为JDBC驱动程序与版本4.0及更高版本的JDBC兼容;这意味着驱动程序应该自动注册,您只需
DriverManager.getConnection()
。JDBCURL中的数据库将为您注册。尽管如此,正如前面所说,这里的问题是由于某种原因无法读取zip文件,但我在pom.xml中添加了依赖项。没有zip文件请参阅我的更新答案。我同意,这一行可能有太多的信息需要添加才能理解。