Good afternoon, I run ready .jar on Linux but an error pops up

java.sql.SQLException: No suitable driver found for jdbc: mysql

I use mysql-connector-java-5.1.39-bin to connect to SQL. It is not clear that it is connected in the project and it lies in the jar and runs on windows.

/******************ПОДКЛЮЧАЕМСЯ К БАЗЕ ДАННЫХ*********************/ //JDBC URL, username and password of MySQL server private static final String url = "jdbc:mysql:"; private static final String user = ""; private static final String password = ""; /**************************КОНЕЦ***************************/ // --------ДОБАВЛЕНИЕ ДАННЫХ-------- try{ String query = " INSERT INTO coordinates_centre (id, la, lo\n"// добавляем данные в таблицу +", ti, sp)\n" + "VALUES('"+id+"', '"+la+"', '"+lo+"', '"+ti+"', '"+sp+"');"; // opening database connection to MySQL server con = DriverManager.getConnection(url, user, password); // getting Statement object to execute query stmt = con.createStatement(); // executing SELECT query rs = stmt.executeUpdate(query); } catch (SQLException sqlEx) { sqlEx.printStackTrace(); } finally { //close connection ,stmt and resultset here try { con.close(); } catch(SQLException se) { /*can't do anything */ } try { stmt.close(); } catch(SQLException se) { /*can't do anything */ } } // --------КОНЕЦ-------- 
  • How do you run on Windows and Linux? - Chubatiy
  • @Chubatiy on Windows from the Linux development environment with the command java -jar myprogram.jar - Varg Sieg
  • that's the trouble. It turns out that you do not "pack" the library in your final jar. What do you collect the project? Ant? Maven? - Chubatiy
  • Search for classpath . The java command line can somehow be set using the environment variable. Or the driver jar is also listed among the parameters in the command line. And you can leave the general library in a special folder, I do not remember the name. - Sergey
  • @Chubatiy Eclipse but he lay in the archive checked. - Varg Sieg

2 answers 2

Option run through the classpath, with the library repository:

 CLASSPATH=/home/chubatiy/my/<ИМЯ>.jar:/home/chubatiy/my/libs/ java org.me.Start 

Where:

  1. /home/chubatiy/my/<IMNAME>.jar full path to your JAR file
  2. / home / chubatiy / my / libs / path to library folder
  3. org.me.Start full path to the main class of the application

UPD: everything turned out to be easier. There was not enough driver registration line. Those. must be added before establishing a database connection:

 Class.forName("com.mysql.jdbc.Driver"); 
  • The strangest thing is that it is in the archive and, for reliability, the .jar is still near the lib package with the driver - Varg Sieg
  • I repeat. With what do you collect? Ant? Maven? Give the structure of the output JAR (If Ant) or pom.xml (if Maven) - Chubatiy
  • the Eclipse development environment, the driver lies in the lib package when building in the jar, this folder is included in the classpath document, the address is registered begins with lib - Varg Sieg
  • I also point out the main class when building .jar - Varg Sieg
  • You tried to run, as I indicated in the answer? Still the same mistake? Bring pozh. how you initialize the connection in the code. Completely the entire connection string (you can remove the port, etc.). Are you sure to call Class.forName("com.mysql.jdbc.Driver") before initializing the connection? - Chubatiy

Analogue of the @Chubatiy response via the -cp instead of the CLASSPATH environment variable:

 java -cp "/home/chubatiy/my/<ИМЯ>.jar:/home/chubatiy/my/libs/" org.me.Start 

java -help displays, among other things, its synonym -classpath and the description of this key:

 -cp <class search path of directories and zip/jar files> -classpath <class search path of directories and zip/jar files> A : separated list of directories, JAR archives, and ZIP archives to search for class files. 

You need to transfer in it separated by a symbol : paths to directories and / or to separate JAR-ZIP files containing the required classes.