Maven - java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

前端 未结 5 403
旧巷少年郎
旧巷少年郎 2020-12-10 04:58

I have a Java app based on Maven, and want to connect to MySQL server.

My pom has:


    mysql
    &l         


        
5条回答
  •  南方客
    南方客 (楼主)
    2020-12-10 05:48

    Change the scope to compile:

    
        mysql
        mysql-connector-java
        5.1.17
        jar
        compile
    
    

    Which - since it is the default scope corresponds to leaving away scope definition at all - same counts for the type:

    
       mysql
       mysql-connector-java
       5.1.17
    
    

    Have a look at this: https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html for detailed information on scoping.

    Here is a quick info for your background:

    You specified the JDBC driver to have a scope runtime. Most IDEs will anyways ignore the scopes and add all of your dependencies to their classpath (e.g. the classpath used when you run something outside of eclipse. By the scope runtime you are telling maven that it must not pack that dependeny into your final jar since the execution environment will "provide that dependency at runtime. E.g. you would either have to manually add it to the classpath when calling your jar or change the scope to compile which will lead to the driver-jar beeing packed inside your jar and available at runtime.

提交回复
热议问题