【问题标题】:The unnamed module reads package org.apache.log4j from both slf4j.log4j12 and log4j未命名模块从 slf4j.log4j12 和 log4j 读取包 org.apache.log4j
【发布时间】:2020-08-28 11:45:04
【问题描述】:

我已经关注this video 在 gradle java 项目中使用 javafx。在我尝试添加 JOpenGeocoding 依赖项之前一切正常:

build.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'
}

Main.java

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);
    }
}

这就是

module-info.java 文件

module TroppAdvisorDesktop.main {
     requires javafx.controls;
     requires javafx.fxml;
     requires jopencage;

     opens sample;
}

不幸的是,当我构建这个项目时,会弹出几个错误,如下所示:errors

  • 我该如何解决?

我注意到,如果我从 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 Barcelona,​​ España

SLF4J:无法加载类“org.slf4j.impl.StaticLoggerBinder”。 SLF4J:默认为无操作(NOP)记录器实现 SLF4J:详情请见http://www.slf4j.org/codes.html#StaticLoggerBinder

【问题讨论】:

    标签: java gradle javafx


    【解决方案1】:

    根据Java Platform Module System Requirements,不允许来自不同模块的同名包相互干扰。

    见:

    互不干扰 — Java 编译器、虚拟机和运行时系统必须确保包含同名包的模块不会相互干扰。如果两个不同的模块包含同名的包,那么从每个模块的角度来看,该包中的所有类型和成员都仅由该模块定义。一个模块中该包中的代码不得访问另一个模块中该包中的包私有类型或成员。

    因为您的某些传递依赖项不是模块化的,所以它们最终会出现在未命名的模块(类路径)上,它们的包名会干扰这些模块。在非模块化 Java 9+ 项目中这不是问题的事实实际上是 java compiler bug

    在 Gradle 中,您可以尝试使用导致冲突的 excluding one of the dependencies 作为解决方法。但请记住,这可能会导致运行时出现不良行为。

    更好的长期解决方案是联系引起冲突的库的开发人员并敦促他们修复它。

    您可以使用 Gradle dependencyInsight 来确定有问题的依赖关系,例如

    gradle dependencyInsight --configuration runtimeClasspath --dependency log4j
    

    【讨论】:

      猜你喜欢
      • 2023-03-17
      • 1970-01-01
      • 2018-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多