简体   繁体   中英

Unable to load class: org.mysql.jdbc.Driver

I am trying to connect a fairly standard dropwizard app to a mysql database. It seems like a lot of people have been having this problem but I cannot figure out what is wrong with my project. It seems like adding in mysql-connector should have worked for me but when I run the following I still get Unable to load class: org.mysql.jdbc.Driver.

java -jar target/bitly-1.0-SNAPSHOT.jar server src/config.yml

Does anyone see what might be incorrect. I also found a lot of responses of adding this to my classpath in IntelliJ and I did that by going to File > Project Structure > Libraries and then adding in the mysql maven dependency but still won't work.

Is there something I am missing?

Full link to repo here. https://github.com/Jazilch/bit.ly

If you are creating the jar using IntelliJ, make sure you included all available elements from your Maven's dependency. To do so, go to:

File > Project Structure > Artifacts

Now select (or add a new jar artifact) all available elements found on the right pane, by double clicking on each one of them all select or select them all, right click in any of the highlighted elements and choose Put into output root . The screen should look something like the one below.

在此处输入图片说明

Now, just rebuild your artifacts and this should do the trick.

short

If You are using maven, just try: mvn exec:java -Dexec.mainClass="com.example.Main"

long

I suspect that the bitly-1.0-SNAPSHOT.jar does not contain all dependencies ("fat jar", like build by spring-boot). So the problem is, that java -jar target/bitly-1.0-SNAPSHOT.jar server src/config.yml does not include 3rd-party-dependencies in the classpath. mvn exec:java ... builds a classpath with all dependencies defined in the pom. With the JVM You add 3rd-Party-Jars to the classpath with the -cp option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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