繁体   English   中英

未命名模块从 slf4j.log4j12 和 log4j 读取包 org.apache.log4j

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM