1

When I run my code program JAVA MAVEN on neatbean it worked.But when I build and run file jar it then appeared error in cmd

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

java.sql.SQLEXception : No suiteable driver found for jdbc:mysql

I have found the error on stackoverflow for file jar maven java,but failed

public Connection connect() {
    //<editor-fold defaultstate="collapsed" desc="comment">
    try {
        String connectionURL = "jdbc:mysql://" + localhost + ":3306/" + database + "?useUnicode=true&characterEncoding=utf-8";
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection(connectionURL, username, pass);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return connection;
}

In the configuration pom.xml file I used

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.37</version>
    </dependency>
Ma Tâm
  • 307
  • 4
  • 20
  • 2
    How do you set your classpath when you run the jar? Are dependencies bundled in? – Thilo Oct 28 '15 at 00:52
  • 3
    http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven – Thilo Oct 28 '15 at 00:52
  • 1
    Yes.I installed classpath,It has run successfully,but because of my program database connected to it was a error when run file jar – Ma Tâm Oct 28 '15 at 01:03
  • 2
    You have to set up these things yourself when you run outside of the IDE. Easiest way is to build an executable jar with dependencies. (easiest to run, not easiest to build...) – Thilo Oct 28 '15 at 01:10
  • Did you mean to use java maven as a library and use java swing to use it? – Ma Tâm Oct 28 '15 at 01:23
  • 1
    before you run your program do you see the jar file for mysql connector (mysql-connector-java-5.1.37-bin.jar) in one of your lib folders ? – mohamed sulibi Oct 28 '15 at 08:08
  • Thilo if you are reading this article, let me thank you very much. I have read your article and I've found answers for your answer – Ma Tâm Oct 28 '15 at 13:35

0 Answers0