java.lang.ClassNotFoundException: com.mysql.jdbc.Driver (maven + jboss)

前端 未结 4 1543
轻奢々
轻奢々 2021-01-21 08:02

maven pom.xml



        
4条回答
  •  遥遥无期
    2021-01-21 08:32

    I had a slightly different use case. I was using apache's BasicDataSource in a stand-alone application.

    I got this exception because I was calling setDriverClassName with com.mysql.jdbc.Driver.class.toString() instead of "com.mysql.jdbc.Drive" as a String (so compile time check ensures that there is no typo or Driver's location didn't change in a version upgrade... etc).

    But!

    When you look Driver's implementation it has a static initialization block:

    static {
      try {
        java.sql.DriverManager.registerDriver(new Driver());
      } catch (SQLException E) {
        throw new RuntimeException("Can't register driver!");
      }
    }
    

    When I wrote com.mysql.jdbc.Driver.class.toString() this initialization block executed too early and caused later on misleading ClassNotFoundException to be thrown. Once I switched to "com.mysql.jdbc.Driver" my application started to work.

    This is a case when static becomes evil :)

提交回复
热议问题