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

4

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

The mysql Driver is in the path below:

c:\jars\jdbc_mysql.jar

The main class of the application is in the package below:

br\com\vl1\principal\SistemaCadastro.java

Running as below, the system works:

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

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

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 is the exception generated when trying to save a record to the database:

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

The entire process was run from the command prompt. I am not using IDE and would like a command line solution.

If someone can help me, I'll thank you right away.

    
asked by anonymous 25.11.2016 / 08:59

1 answer

5

The "nail" solution would include your jar and the MySQL driver jar in Classpath and then run 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 the convenience of the end user you can set up an entry point and the Classpath in 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 requiring caution; there are some legal and technical issues to consider when we want to do this. If you go this way it's worth taking a look at tools like Maven Shade Plugin .

SOURCES:

25.11.2016 / 10:56