Hello, there is a class that connects to the database. Using eclipse I create a jar file in which this class and mysql connection, when I run jar, I constantly see the error java.lang.ClassNotFoundException: com.mysql.jdbc:Driver tell me what am I doing wrong?

 import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Solution1 { private static Connection connection = null; private static Statement statement = null; private static String url = "jdbc:mysql://localhost:3306/example?autoReconnect=true&useSSL=false"; public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection(url, "admin", "admin"); statement = connection.createStatement(); PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM Example"); ResultSet resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { System.out.println(resultSet.getString(2)); } } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } 

as a result, jar contains:

  1. main package in which class
  2. jar file connection
  3. META-INF folder (generated automatically)
  • Add a connection code, use code, etc. - Rostislav Dugin
  • what, absolutely no thoughts why a mistake? - user238137
  • Well .... Not connected class. - Rostislav Dugin
  • If you try to register the import, will it work out? Is there such a class? - Rostislav Dugin
  • @RostislavDugin dabavil and imports. the fact is that everything works fine in the eclipse, the problem only occurs when I create the jar file - user238137

1 answer 1

The problem is that when you build a project, the libraries are not added to the final executable file.

To run your executable file:

  1. Or write the path to .jar in the launch path.
  2. Or add to pom.xml (if you use Maven:

     <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>com.пакет.Main</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> 

    Main jar-with-dependencies make-assembly package single

Or you can put all the dependencies in a separate folder:

  <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>${plugin.dependency.version}</version> <executions> <execution> <id>copy-dependencies</id> <phase>prepare-package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${distrib.directory}/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin> 
  • I understand in pom.xml you are packing all dependencies in one .jar-nickname? - I. Perevoz
  • @ I.Perevoz, yes. - Rostislav Dugin