I am a bit new to maven and javafx. The program behaves normally whenever it is running through eclipse. However, a problem occurs whenever I clean install it into jar using maven. i come across java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
error message. Note that the javafx part of my code runs just fine. The program starts with a login instance which opens just fine, but when I try to connect to the MySQL database by pressing "login", the following error occurs: -
java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Unknown Source)
at x.xx@0.0.1-SNAPSHOT/x.xx.xxx.MySQL_Connect.Login(Unknown Source)
at x.xx@0.0.1-SNAPSHOT/x.xx.xxx.Login_Control$1.handle(Unknown Source)
at x.xx@0.0.1-SNAPSHOT/x.xx.xxx.Login_Control$1.handle(Unknown Source)
at javafx.base@19.0.2.1/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at javafx.base@19.0.2.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at javafx.base@19.0.2.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at javafx.base@19.0.2.1/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at javafx.base@19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base@19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base@19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base@19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base@19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base@19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base@19.0.2.1/com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at javafx.base@19.0.2.1/com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.base@19.0.2.1/javafx.event.Event.fireEvent(Unknown Source)
at javafx.graphics@19.0.2.1/javafx.scene.Node.fireEvent(Unknown Source)
at javafx.controls@19.0.2.1/javafx.scene.control.Button.fire(Unknown Source)
at javafx.controls@19.0.2.1/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
at javafx.controls@19.0.2.1/com.sun.javafx.scene.control.inputmap.InputMap.handle(Unknown Source)
at javafx.base@19.0.2.1/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at javafx.base@19.0.2.1/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at javafx.base@19.0.2.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at javafx.base@19.0.2.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at javafx.base@19.0.2.1/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at javafx.base@19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base@19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base@19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base@19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base@19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at javafx.base@19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at javafx.base@19.0.2.1/com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at javafx.base@19.0.2.1/com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.base@19.0.2.1/javafx.event.Event.fireEvent(Unknown Source)
at javafx.graphics@19.0.2.1/javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.graphics@19.0.2.1/javafx.scene.Scene.processMouseEvent(Unknown Source)
at javafx.graphics@19.0.2.1/javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at javafx.graphics@19.0.2.1/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at javafx.graphics@19.0.2.1/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Unknown Source)
at javafx.graphics@19.0.2.1/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(Unknown Source)
at javafx.graphics@19.0.2.1/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
at javafx.graphics@19.0.2.1/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at javafx.graphics@19.0.2.1/com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at javafx.graphics@19.0.2.1/com.sun.glass.ui.View.notifyMouse(Unknown Source)
at javafx.graphics@19.0.2.1/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics@19.0.2.1/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)
However, you can see that the library is in target\modules\ as below: -
This is the target\modules\ directory
My pom.xml is as follows: -
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>x.xx</groupId>
<artifactId>xxx</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>xxx</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mainClass>x.xx.xxx.Main</mainClass>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<mysql.version>8.0.32</mysql.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-web</artifactId>
<version>19.0.2.1</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>19.0.2.1</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-media</artifactId>
<version>19.0.2.1</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>19.0.2.1</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>19.0.2.1</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>${mysql.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<configuration>
<target>
<echo file="${project.build.directory}/xx.bat">start jlink-image\bin\java -jar modules\${project.artifactId}-${project.version}.jar</echo>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<release>11</release>
</configuration>
<dependencies>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>9.5</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
<outputDirectory>${project.build.directory}/modules</outputDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.moditect</groupId>
<artifactId>moditect-maven-plugin</artifactId>
<version>1.0.0.Beta2</version>
<executions>
<execution>
<id>create-runtime-image</id>
<phase>package</phase>
<goals>
<goal>create-runtime-image</goal>
</goals>
<configuration>
<modulePath>
<path>${project.build.directory}/modules</path>
</modulePath>
<modules>
<module>${project.groupId}</module>
</modules>
<launcher>
<name>${project.groupId}</name>
</launcher>
<compression>2</compression>
<stripDebug>true</stripDebug>
<outputDirectory>${project.build.directory}/jlink-image</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.6.0</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/modules</outputDirectory>
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
here also is module-info.java, if that helps:-
module x.xx{
requires javafx.controls;
requires javafx.fxml;
requires javafx.media;
requires java.sql;
opens x.xx.xxx to javafx.fxml;
exports x.xx.xxx;
}
and below is a snippet of MySQL_Connect.java which involves connecting to the server: -
package x.xx.xxx;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import javafx.scene.control.Alert;
public class MySQL_Connect {
static String url = "jdbc:mysql://192.168.100.2:3306/xxx";
static String username = "user";
static String password = "P@ssW0rd";
public static boolean Login(String usern, String pass) {
Connection mysql = null;
PreparedStatement query = null;
ResultSet reply = null;
try{
Class.forName("com.mysql.cj.jdbc.Driver");
mysql = DriverManager.getConnection(url,username,password);
query = mysql.prepareStatement("SELECT * FROM xxx.users WHERE username = ?");
query.setString(1, usern);
reply = query.executeQuery();
..........
A few things i tried included:
- playing with the Java Build Path in eclipse and add the mysql connector externally in the Modulepath and in the Classpath.
- playing with the module-info.java file to see if maybe maven needs a different definition of java.sql;
- utilizing different plugins on maven that looked promising including shading and the stuff you see above.
- tried to upgrade/downgrade the connector, other plugins, and the jdk versions.
- added the "Class.forName" in the MySQL_Connect.java (a different error saying that it could not find a suitable driver to connect me to the database).
- uninstalling and reinstalling eclipse | maven | jdk.
i went through various stackoverflow threads that went through similar problems to this one but nothing seems to work. This is my first post ever on stackoverflow, i hoped i wouldn't need to do this, but i am running out of options at this point. :S