【问题标题】:Gradle doesn't recognize mainModule properlyGradle 无法正确识别 mainModule
【发布时间】:2020-06-08 13:30:16
【问题描述】:

我有一个简单的 javaFx 项目。我决定创建一个模块信息来为我的项目需要 javaFX 库。在此之前我的程序运行良好,但现在 getClass().getClassLoader().getResource(resPath) 无法从我的资源中获取资源。每次运行我的应用程序时,我都会收到此消息:

没有为主类提供模块,假设是当前模块。最好以以下格式提供“mainClassName”:“$moduleName/a.b.Main”

这是我的 build.gradle:

plugins {
    id 'java-library'
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.8'
}

version '1.0'

repositories {
    jcenter()
}

plugins.withType(JavaPlugin).configureEach {
    java {
        modularity.inferModulePath = true
    }
}


tasks.withType(Test).configureEach {
    useJUnitPlatform()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
}

application {
    mainModule = 'liug'
    mainClass = 'render.Main'
}

javafx {
    version = "13"
    modules = ['javafx.controls', 'javafx.fxml']
}

还有模块信息:

module moduleInfo {
    requires javafx.base;
    requires javafx.fxml;
    requires javafx.controls;
    requires javafx.graphics;


    opens controller to javafx.base, javafx.fxml, javafx.controls, javafx.graphics;
    opens core to javafx.base, javafx.fxml, javafx.controls, javafx.graphics;
    opens popups to javafx.base, javafx.fxml, javafx.controls, javafx.graphics;
    opens render to javafx.base, javafx.fxml, javafx.controls, javafx.graphics;
    opens stages to javafx.base, javafx.fxml, javafx.controls, javafx.graphics;
    opens view to javafx.base, javafx.fxml, javafx.controls, javafx.graphics;

    exports controller;
    exports core;
    exports popups;
    exports render;
    exports stages;
    exports view;
    exports core.buildings;
}

here is my projects structure

谁能说我做错了什么?

【问题讨论】:

  • javafx-gradle-plugin 也应用了gradle-modules-plugin,这可能与 6.4 中添加的新模块功能 Gradle 不兼容。如果他们尚未解决新版本的问题,您可能需要考虑向其中一个或两个提交问题。
  • 我发现 javaFX 提供了通过 gradle (openjfx.io/openjfx-docs/#IDE-Intellij) 制作模块化 JavaFX 项目的机会。我按照说明在build.gradle中将application{..}替换为mainClassName = "$moduleName/Main"。现在想法抛出:错误:Could not find or load main class Main in module render有什么想法吗?
  • 它是module/main-class,这意味着我认为在你的情况下它应该是liug/render.Main
  • 当问题是:原因:java.lang.module.InvalidModuleDescriptorException: Package liug.render not found in module
  • 我已经多次更改module-info名称,发现这个sn-p:mainClassName = "$moduleName/Main"找到了一个与module-info名称同名的包。例如,如果我将 projectCityBuilder 设置为模块信息的名称,我的程序将抛出:Caused by: java.lang.module.InvalidModuleDescriptorException: Package projectCityBuilder not found in module

标签: java gradle javafx


【解决方案1】:

问题出在项目结构中,就像在 gradle 版本中所说的那样。我在 gradle-wrapper.properties 中写了这个 sn-p:distributionUrl=https\://services.gradle.org/distributions/gradle-5.0-all.zip 并用 fxml 资源 (how it looks like) 更改了包的名称。我还更改了用于获取 FXML 文件的资源的路径。这就是现在的样子:

URL url = getClass().getResource("MaiMenu.fxml");
            Parent root = FXMLLoader.load(url);
            stage.setScene(new Scene(root));
            stage.show();

This tutorial 帮了我很大的忙。 我仍然有一些问题,但我认为它们与此问题无关,因此我将其关闭。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-06
    • 2021-05-13
    • 1970-01-01
    • 2020-05-03
    • 1970-01-01
    • 1970-01-01
    • 2018-07-18
    • 2014-08-07
    相关资源
    最近更新 更多