0

i got a ClassNotFoundException with this code:

try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    MySQL.connect();

I imported the mysql-connector (http://dev.mysql.com/downloads/connector/j/5.0.html) and added it in a special folder (I named it External) and added it to the build-path. When I launch it in Eclipse it works fine for me but if I export it as jar and launch it by java -jar it does not work to me and I get the ClassNotFoundException... Hope someone could help me :) Mabye it has to do something with the classpath but how do I edit it because -cp External/mysql-connector.jar does not work..

2 Answers2

0

When you use java -jar the -cp option is ignored. See the tool documentation. You have to list the dependent JARs in the Class-path entry of the Manifest.

However the Class.forName() line hasn't been needed since 2007.

user207421
  • 305,947
  • 44
  • 307
  • 483
-1

I also think you have to config the imported library by [-cp] like

java -cp ../lib/mysql-connector.jar;../lib/* ・・・
Ming
  • 211
  • 1
  • 6
  • java -cp External/mysql-connector-java-5.1.18-bin.jar -Xms512M -Xmx1536M -jar test.jar -- this does not work and java -cp ../External/mysql-connector-java-5.1.18-bin.jar -Xms512M -Xmx1536M -jar test.jar does also not work.. – Julian Lüders Jun 14 '16 at 09:12
  • what about: [java -Xms512M -Xmx1536M -cp test.jar;External/mysql-connector-java-5.1.18-bin.jar test.main] test.main is the main method of your test.jar – Ming Jun 15 '16 at 03:20