我的项目使用JavaFX和MySQL连接器j-9,它位于IDEA项目文件夹内的lib文件夹中。我每天通过复制 IDEA 项目文件夹并重命名来备份该项目。
当我在 IDE 中打开新文件夹时,第一次构建/运行失败,并显示错误“未找到合适的驱动程序”。尽管驱动程序显示在外部库中,但情况仍然如此。我必须转到“项目结构”、“库”,滚动到 mySQL 连接器,右键单击并选择“添加到模块”。然后它构建/运行良好。
我不明白为什么当我复制整个文件夹时,MySQL 连接器会从模块中丢失。有没有办法让连接器进入模块并保持在那里?
当这种情况第一次发生时,我能够进行故障排除,直到找到添加到模块的需要,但这种情况每次都会发生,我无法进一步采取措施。
POM.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>JE.Spex</groupId>
<artifactId>Drift</artifactId>
<version>1.0</version>
<name>Drift</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>5.8.2</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>18</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>18</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-web</artifactId>
<version>18</version>
</dependency>
<dependency>
<groupId>org.controlsfx</groupId>
<artifactId>controlsfx</artifactId>
<version>11.1.1</version>
</dependency>
<dependency>
<groupId>com.dlsc.formsfx</groupId>
<artifactId>formsfx-core</artifactId>
<version>11.3.2</version>
<exclusions>
<exclusion>
<groupId>org.openjfx</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.kordamp.ikonli</groupId>
<artifactId>ikonli-javafx</artifactId>
<version>12.3.1</version>
</dependency>
<dependency>
<groupId>org.kordamp.bootstrapfx</groupId>
<artifactId>bootstrapfx-core</artifactId>
<version>0.4.0</version>
</dependency>
<dependency>
<groupId>eu.hansolo</groupId>
<artifactId>tilesfx</artifactId>
<version>11.48</version>
<exclusions>
<exclusion>
<groupId>org.openjfx</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>uk.co.caprica</groupId>
<artifactId>vlcj</artifactId>
<version>4.8.3</version>
</dependency>
<dependency>
<groupId>uk.co.caprica</groupId>
<artifactId>vlcj-javafx</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>18</source>
<target>18</target>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<executions>
<execution>
<!-- Default configuration for running with: mvn clean javafx:run -->
<id>default-cli</id>
<configuration>
<mainClass>je.spex.MainApp</mainClass>
<launcher>app</launcher>
<jlinkZipName>app</jlinkZipName>
<jlinkImageName>app</jlinkImageName>
<noManPages>true</noManPages>
<stripDebug>true</stripDebug>
<noHeaderFiles>true</noHeaderFiles>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<!-- put your configurations here -->
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>je.spex.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
模块信息.java
module je.spex.Drift {
requires javafx.controls;
requires javafx.fxml;
requires javafx.web;
requires org.controlsfx.controls;
requires com.dlsc.formsfx;
requires org.kordamp.ikonli.javafx;
requires org.kordamp.bootstrapfx.core;
requires eu.hansolo.tilesfx;
requires java.sql;
requires java.desktop;
requires java.net.http;
requires org.jetbrains.annotations;
requires uk.co.caprica.vlcj;
opens je.spex.Drift to javafx.fxml;
exports je.spex.Drift;
}
您有多个问题:
此示例项目修复了上述问题。
pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>mysqlfx</artifactId>
<version>1.0-SNAPSHOT</version>
<name>mysqlfx</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>23</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>9.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<source>21</source>
<target>21</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
src/main/java/module-info.java
不幸的是,从版本9开始,MySQL驱动程序仍然是一个“自动模块”,因此它不能与诸如“jlink
”之类的打包工具一起使用。 作为一个元数据中没有任何模块信息的自动模块,模块名称
mysql.connector.j
源自jar文件名。
module org.example.mysqlfx {
requires javafx.controls;
requires mysql.connector.j;
requires java.sql;
exports org.example.mysqlfx;
}
src/main/java/org/example/mysqlfx/MySqlDriverVersionApp
通常您不会像这样直接实例化 SQL 驱动程序。 现代 JDBC 实现足够智能,可以从 JDBC URL 确定要使用的驱动程序。 但出于演示目的,我直接在此处访问驱动程序。
package org.example.mysqlfx;
import com.mysql.cj.jdbc.Driver;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import java.sql.SQLException;
public class MySqlDriverVersionApp extends Application {
@Override
public void start(Stage stage) throws SQLException {
Driver driver = new Driver();
String driverVersion = driver.getMajorVersion() + "." + driver.getMinorVersion();
Label info = new Label(
"MySQL Driver Version: " + driverVersion
);
info.setStyle("-fx-font-size:20px");
info.setPadding(new Insets(20));
stage.setScene(new Scene(info));
stage.show();
}
public static void main(String[] args) {
launch();
}
}