-1

I am new to programming, of which I do not know much, I ask for help to verify why this problem occurs: if I run the program on the "eclipse" platform, everything works correctly, but when I export and execute it, this error occurs: java. lang.ClassNotFoundException: com.mysql.jdbc.Driver

This is the code:

public void createTableRegister() {
    String tableName="register";
    String columnType1="VARCHAR(60)";
    String columnType2="INT(30)";
    String column1="Apellidos";
    String column2="Nombres";
    String column3="DNI";
    String column4="Correo";
    String column5="Contraseña";
    String query="CREATE TABLE IF NOT EXISTS `"+tableName+"`(`"+column1+"` "+columnType1+", `"+column2+"` "+columnType1+", `"+column3+"` "+columnType2+", `"+column4+"` "+columnType1+", `"+column5+"` "+columnType1+")";
    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance(); 
        String url="jdbc:mysql://"+getHost()+":"+getPort()+"/"+getDatabase();
        String un=getUsername();
        String ps=getPassword();
        con=DriverManager.getConnection(url,un,ps);
        Statement stmt = con.createStatement();
        stmt.executeUpdate(query);
    } catch (Exception e) {
        System.out.println("ERROR CreateTableRegister: "+e);
    }
}
  • do you know what a ClassNotFoundException is? – Stultuske May 24 '19 at 06:30
  • You have probably added the jdbc jar manually to your eclipse project. When running the project from eclipse it is able to found the mentioned class inside the jar, but when you re exporting the project you are probably only exporting your code without the dependencies hence the missing class. You need to build a jar containing all the dependencies or try putting the jdb jar in the same directory as your application. – anthony yaghi May 24 '19 at 06:33
  • How are you running your application? – Riaan Nel May 24 '19 at 06:38
  • @anthonyyaghi How can I build a jar with all the dependencies? – Luis Alva Celis May 24 '19 at 06:43
  • @RiaanNel What do you mean? – Luis Alva Celis May 24 '19 at 06:46

1 Answers1

0

The probelm must be, that you are not exporting the Driver. If you export your applicatoin in eclipse, make sure that the jar-File is selected and also packed in the jar-File you create.

A ClassNotFoundException means, that java can not find a class with this name on the class path.

heiwil
  • 612
  • 4
  • 8
  • and how can I export the driver? I do not know much about exporting programs in java – Luis Alva Celis May 24 '19 at 06:48
  • Right click on your project in eclipse -> `Export` -> Select `Java` -> `Runnable JAR file` -> Click on `Next`. Here you can select the behaviour of `Library Handling`. E.g. select `Extract required libraries into generated JAR`. – heiwil May 24 '19 at 06:59
  • You can open the .jar-File e.g. with 7zip, WinZip etc. Its also a packed file like .rar, .zip etc. There you can see, if the needed classes were copied in your .jar-file – heiwil May 24 '19 at 07:00
  • Merging jars into an uber-jar is generally the wrong approach and can break if you use multiple JDBC drivers if you don't properly merge their service definition. – Mark Rotteveel May 24 '19 at 10:37