James Zilch
James Zilch

Reputation: 1225

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

Upvotes: 1

Views: 858

Answers (2)

Jens Schumacher
Jens Schumacher

Reputation: 46

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.

Upvotes: 1

Alain Cruz
Alain Cruz

Reputation: 5097

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.

enter image description here

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

Upvotes: 1

Related Questions