[英]The unnamed module reads package org.apache.log4j from both slf4j.log4j12 and log4j
我已按照此視頻在 gradle java 項目中使用 javafx。 一切正常,直到我嘗試添加 JOpenGeocoding 依賴項:
構建.gradle
plugins {
id 'java'
id 'org.openjfx.javafxplugin' version '0.0.7'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
jcenter()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile("com.byteowls:jopencage:1.3.0")
compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.29'
}
javafx {
modules = [ 'javafx.controls', 'javafx.fxml' ]
version = '11.0.2'
}
和
主程序
package sample;
import com.byteowls.jopencage.JOpenCageGeocoder;
import com.byteowls.jopencage.model.JOpenCageResponse;
import com.byteowls.jopencage.model.JOpenCageReverseRequest;
import javafx.application.Application;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
JOpenCageGeocoder jOpenCageGeocoder = new JOpenCageGeocoder("dba40429c8ae43b78ae293bc0d221fb5");
JOpenCageReverseRequest request = new JOpenCageReverseRequest(41.40015, 2.15765);
request.setLanguage("es"); // prioritize results in a specific language using an IETF format language code (espanol)
request.setNoDedupe(true); // don't return duplicate results
request.setLimit(5); // only return the first 5 results (default is 10)
request.setNoAnnotations(true); // exclude additional info such as calling code, timezone, and currency
request.setMinConfidence(3); // restrict to results with a confidence rating of at least 3 (out of 10)
JOpenCageResponse response = jOpenCageGeocoder.reverse(request);
// get the formatted address of the first result:
String formattedAddress = response.getResults().get(0).getFormatted();
System.out.println(formattedAddress);
}
}
這是
模塊信息.java文件
module TroppAdvisorDesktop.main {
requires javafx.controls;
requires javafx.fxml;
requires jopencage;
opens sample;
}
不幸的是,當我構建這個項目時,會彈出幾個錯誤,如您所見:錯誤
我注意到,如果我從build.bradle
文件中刪除compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.29'
,它會正確執行,但無論如何都會出現一些錯誤,例如:
警告:未知枚舉常量 Include.NON_NULL 原因:com.fasterxml.jackson.annotation.JsonInclude$Include 的類文件未找到 1 個警告
任務:Main.main()
Travessera de Gràcia, 142, 08001 巴塞羅那, 西班牙
SLF4J:無法加載類“org.slf4j.impl.StaticLoggerBinder”。 SLF4J:默認為無操作 (NOP) 記錄器實現 SLF4J:有關更多詳細信息,請參閱http://www.slf4j.org/codes.html#StaticLoggerBinder 。
根據Java Platform Module System Requirements ,同名但來自不同模塊的包之間不允許相互干擾。
看:
互不干擾——Java 編譯器、虛擬機和運行時系統必須確保包含同名包的模塊不會相互干擾。 如果兩個不同的模塊包含同名的包,那么從每個模塊的角度來看,該包中的所有類型和成員都僅由該模塊定義。 一個模塊中該包中的代碼不能訪問另一個模塊中該包中的包私有類型或成員。
因為您的一些傳遞依賴項不是模塊化的,所以它們最終位於未命名的模塊(類路徑)上,它們的包名稱會在那里干擾。 在非模塊化 Java 9+ 項目中這不是問題的事實實際上是一個java 編譯器錯誤。
在 Gradle 中,您可以嘗試排除導致沖突的依賴項之一作為解決方法。 但請記住,這可能會在運行時導致不需要的行為。
更好的長期解決方案是聯系導致沖突的庫的開發人員並敦促他們修復它。
您可以使用 Gradle dependencyInsight
來確定有問題的依賴項,例如
gradle dependencyInsight --configuration runtimeClasspath --dependency log4j
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.