Still get compile error ClassNotFoundException
from com.mysql.cj.jdbc.Driver
from java-8 mysql-connector-java-5.1.147.jar
and mysql-connector-java-5.1.47-bin.jar
(though most postings say it is the bin.jar that is needed)
I unpacked the tar file and copied the two mysql-connector jar files to /usr/lib/jvm/java-8-oracle/lib
and to my execution folder /var/www/html
for overkill Also, I put classpath in /etc/environment
, and in java file and when compiling (tried each separately first).
then compiled, tried with and without in compile
Get the same error in all these cases (and other permutations):
Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
... initially called from bbfq.main
Any other ideas of where I'm not coding the classpath correctly? (this posting may not show the spacing and indents of code as I wrote it)
Thanks.
In /etc/environment:
JAVA_HOME="/usr/lib/jvm/java-8-oracle/bin"
export JAVA_HOME
CLASSPATH=".:/usr/lib/jvm/java-8-oracle/lib:/var/www/html"
export CLASSPATH
in java file:
import java.sql.*;
import java.io.*;
public class bbfq {
....
Connection con=null;
...
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql//localhost:3306/drupal7" "id" "password");
....
Manifest file bbfq.mf:
Manifest-Version: 1.0
Class-Path: phidget22.jar mysql-connector-java-5.1.47.jar mysql-connector-java-5.1.47-bin.jar
compiled with and without declaring classpath:
sudo javac -classpath bbfq.java
or
sudo javac -classpath .:mysql-connector-java-5.1.47.jar:mysql-connector-java-5.1.47-bin.jar bbfq.java
and tried to run with or without classpath:
sudo java -jar bbfq.jar
or
sudo java -classpath .:bbfq:mysql-connector-java-5.1.47-bin.jar -jar bbfq.jar
Thanks.
First, remove Class.forName("com.mysql.ch.jdbc.Driver");
. That has not been necessary for many years, because JDBC drivers are located using the service provider process.
Even if it were warranted, your Class.forName call assumes the driver class is com.mysql.ch.jdbc.Driver. That is no longer the case, and in fact there is no such class in the driver .jar file, so the Class.forName call will always fail.
Second, the -classpath
option is ignored when using the -jar
option. From the documentation :
When you use
-jar
, the specified JAR file is the source of all user classes, and other class path settings are ignored.
When you use the -jar
option, only the manifest's Class-Path
attribute is used to add other files to the classpath.
The Class-Path
attribute is actually a list of relative URLs , separated by spaces. They are relative to the .jar file containing the manifest.
So, you must place mysql-connector-java-5.1.47-bin.jar in the same directory as the bbfq.jar for it to be picked up by the classloader when it reads the Class-Path manifest attribute.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.