0

This question has asked many times, but this one is slightly different. In my case, I'm not getting any error when running from Eclipse, it successfully runs and is able to connect to MySQL.

I have a Maven project in Eclipse, using the MySQL database and MySQL Connector/J for connection.

However, after deploying to jar I am getting the error.

I am using mvn clean install to deploy

I have added dependency in pom.xml like:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.49</version>
    <scope>runtime</scope>
</dependency>

I am using following command to run the jar file java -jar ./target/project.jar

but this gives

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

What's wrong here?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Please show the pom.xml of your project. It sounds like you haven't configured Maven to generate the appropriate `Class-Path` entry in the manifest, and put the dependencies in the right location. – Mark Rotteveel Feb 13 '21 at 11:02

2 Answers2

1

Try to change the scope from runtime to compile or delete String scope at all. This String talks about that this dependency don't load in compile time. When you call maven install then happens source compile and this dependency don't add to classpath.

Dmitrii B
  • 2,672
  • 3
  • 5
  • 14
0

Remove <scope>runtime</scope> from the mysql maven dependency as it makes the jar available only in the runtime. Either you don't put the scope at all or put that in compile time.

Do mvn clean install. It will work.

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

Also, you need to create an executable fat jar using maven. Normal jar created by maven is non-executable. It won't work.

To create a executable jar by maven. Refer here.

Anish B.
  • 9,111
  • 3
  • 21
  • 41