我正在 IntelliJ 中使用 JavaFX 創建一個示例演示應用程式,但我需要使用一個名為 JavaFaker 庫的庫。我使用 Gradle 作為構建系統,但每次嘗試添加庫時,無論是作為 build.gradle 檔案中的實作,還是通過 IntelliJ 專案結構選項,module.java 檔案都會顯示錯誤:找不到模塊。我已經嘗試將其添加到模塊中,但沒有任何變化。
模塊資訊.java
module com.example.demo1 {
requires javafx.controls;
requires javafx.fxml;
requires javafaker;
opens com.example.demo1 to javafx.fxml;
exports com.example.demo1;
}
構建.gradle
plugins {
id 'java'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.10'
id 'org.beryx.jlink' version '2.24.1'
}
group 'com.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
ext {
junitVersion = '5.8.2'
javaFakerVersion = '1.0.2'
}
sourceCompatibility = '17'
targetCompatibility = '17'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
application {
mainModule = 'com.example.demo1'
mainClass = 'com.example.demo1.HelloApplication'
}
javafx {
version = '17.0.1'
modules = ['javafx.controls', 'javafx.fxml']
}
dependencies {
implementation("com.github.javafaker:javafaker:${javaFakerVersion}")
testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")
}
test {
useJUnitPlatform()
}
jlink {
imageZip = project.file("${buildDir}/distributions/app-${javafx.platform.classifier}.zip") as RegularFile
options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
launcher {
name = 'app'
}
}
jlinkZip {
group = 'distribution'
}
錯誤資訊
> Task :HelloApplication.main() FAILED
Error occurred during initialization of boot layer
java.lang.module.FindException: Module javafaker not found, required by com.example.demo1


uj5u.com熱心網友回復:
我嘗試了一段時間讓它與 Gradle 一起作業,但無法做到。我不太了解 Gradle,但除非你這樣做,否則我不建議嘗試它。
使用 Maven
很容易讓它與 Maven 一起作業。
創建一個新的 JavaFX 專案
- 選擇 Maven 而不是 Gradle 作為構建系統。
將 javafaker 依賴項添加到您的 pom.xml。
<dependency> <groupId>com.github.javafaker</groupId> <artifactId>javafaker</artifactId> <version>1.0.2</version> </dependency>按 Maven 視窗中的重繪 圖示將 Maven 專案重新匯入 IDE。
將 javafaker 模塊的 requires 子句添加到您的 module-info.java
requires javafaker;將使用 javafaker 的代碼添加到您的應用程式中。
我沒有使用 javafaker 的代碼,所以我無法驗證最后一步是否可行,但是,試一試。. .
我在使用 Gradle 時遇到的一些問題
查看Gradle 檔案部分“使用非模塊的庫”:
第三種情況是根本不提供模塊資訊的傳統庫——例如 commons-cli:commons-cli:1.4。Gradle 將此類別庫放在類路徑而不是模塊路徑上。然后類路徑被 Java 視為一個模塊(所謂的未命名模塊)。
您正在使用的 javafaker 依賴項就是這種情況。它沒有module-info.java也沒有在其清單檔案中定義屬性Automatic-Module-Name(這是本節中的其他兩種情況)。其他兩種情況都會導致 Gradle 將庫放在模塊路徑上,但您擁有的情況意味著它位于類路徑上。
當您想從您定義的命名模塊訪問代碼時,這是一個問題,因為您創建了一個module-info.java.
您的模塊只能找到它需要的模塊的代碼和資源(需要在模塊路徑上),因此您添加requires javafaker到module-info.java, 并在嘗試通過 IDE 運行時得到以下資訊:
java.lang.module.FindException: Module javafaker not found, required by com.example.demo1
因此,按照我鏈接的 Gradle 檔案requires javafaker的建議,您從module-info.java我鏈接的 Gradle 檔案中洗掉,當您嘗試編譯時,您會得到以下資訊:
Package 'com.github.javafaker' is declared in module 'javafaker', but module 'com.example.demo1' does not read it
因此,您必須將庫放在 module-info 中才能使用它,但不能將庫放在 module-info 中,因為 Gradle 放入了類路徑 -> catch-22。
有一些解決方法,例如提供 VM 引數以允許訪問未命名的模塊(即類路徑),或者可能以某種方式修改 Gradle 構建和/或 IDE 的模塊路徑處理(我不知道如何),但是他們很丑。
On the other hand, for this case, Maven acts differently from Gradle, it places the dependent library on the module path, even if it does not have a module-info.java or Automatic-Module-Name defined. This means that it was (for me) much easier to set up and use.
Incidental advice on module naming
This is not an error, but note: Although module names with numbers in them are now allowed due to a change in the module system specification, it is probably best not to put numbers in module names to prevent the module name and version info being confused.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/446721.html
