mysql driver is not found when the application is rotated from a jar

Asked

Viewed 176 times

4

I have developed an employee registration system. In this system the data persistence is done in a mysql database. When running the program by the package the system works normally, but when generating an executable jar it does not find the mysql driver.

mysql Driver is in the path below:

c:\jars\jdbc_mysql.jar

The main application class is in the package below:

br\com\vl1\principal\SistemaCadastro.java

Running the way below the system works:

c:\raiz\java -cp c:\jars\jdbc_mysql.jar;. br.com.vl1.principal.SistemaCadastro

But when I run as below it does not find the Mysql Driver

c:\raiz\java -cp c:\jars\jdbc_mysql.jar;. -jar SistemaCadastro.jar

The jar was generated with the following command:

 jar -cvfm SistemaCadastro.jar META-INF\MANIFEST.MF br

Below follows the generated Exception when I try to save a record in the database:

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/sistema_cadastro

The entire process was executed by the command prompt. I am not using IDE and would like a per-command-line solution.

If anyone can help me, I’d appreciate it.

  • Have you compiled the mysql driver jar together? If not, this is probably the cause.

1 answer

5


The "nail" solution would be to include your jar and the Mysql driver jar in Classpath and then rotate the class main:

java -cp c:\jars\jdbc_mysql.jar;c:\raiz\java\SistemaCadastro.jar  br.com.vl1.principal.SistemaCadastro

Of course this solution is more for development. For end-user convenience it is possible to configure an entry point and the Classpath in the MANIFEST.MF.

Let’s say you distribute your application as follows:

raiz/
    lib/
        jdbc_mysql.jar
    SistemaCadastro.jar
        META-INF/
            MANIFEST.MF
        br.com.vl1.principal.SistemaCadastro

Where the content of MANIFEST.MF is:

Manifest-Version: 1.0
Created-By: Vlamir78
Main-Class: br.com.vl1.principal.SistemaCadastro
Class-Path: lib/jdbc_mysql.jar 

In this case the command below would be enough to run your application:

java -jar SistemaCadastro.jar

Update: A third way is to create a Uber Jar which repackages all dependencies together with your application. That being said, this is a more advanced subject that requires caution; there are some legal and technical issues to consider when we intend to do this. If you go that way it’s worth taking a look at tools like the Maven Shade Plugin.


SOURCES:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.