Java.lang.ClassNotFoundException: com.mysql.jdbc.Driver when executing JAR

I am trying to connect to a local MySQL server with the following code:

dbURL = "jdbc:mysql://localhost:3306:/" + dbname; try{ Class.forName("com.mysql.jdbc.Driver"); try{ con = DriverManager.getConnection(dbURL, dbuser, dbpass); } catch (SQLException ex){ System.out.println("ERROR: Could not connection to SQL DB"); con = null; } } catch (ClassNotFoundException e){ System.out.println("Error: "); e.printStackTrace(); } 

Then i get

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

I understand that Java cannot find a suitable driver for connecting the Java environment to the MySQL database. This compiles on Windows 7 and migrates to Ubuntu 11.04.

Is there any specific way to run a Java program with a specific class, for example:

 java -cp /usr/share/java/mysql-connector-java.jar program.jar 

This did not work when I tried.

+4
source share
3 answers

In the case of a JAR, the arguments -cp and -classpath and the environment variable %CLASSPATH% ignored . Instead, the class path should be specified in the Class-Path entry of the native file /META-INF/MANIFEST.MF JAR. This may be the path relative to the JAR itself. For instance. in the same folder or in the /lib subfolder.

The example below assumes that the driver is in the same folder as the JAR.

 Class-Path: mysql-connector-java.jar 

(make sure the MANIFEST.MF file has an empty line at the end)

See also:

+4
source

No matter where it is compiled, it will run smoothly in the same JVM implementation.

All you have to do is correctly include the JDBC driver connector in your classpath. Try putting mysql-connector-java.jar in the same directory with your program.

If you unzip this jar file, does it contain com / mysql / jdbc / Driver.class? If not, try downloading the jdbc driver implementation from the mysql website.

0
source

Install MySQL Connector for JAVA

 sudo apt-get install libmysql-java 

Set class path

 export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java.jar 

Source: http://marksman.wordpress.com/2009/03/01/setting-up-mysqljdbc-driver-on-ubuntu/

0
source

Source: https://habr.com/ru/post/1387816/


All Articles