Javafx找不到自定义模块

发布于 2025-01-21 15:39:29 字数 3014 浏览 0 评论 0 原文

我正在使用OpenJDK 11的Javafx中自己的自定义库制作一个简单的电报机器人。该项目是由Intellij Idea的Javafx项目构建的,Gradle作为构建系统。该库由一个单个类(Telegram)和一个单个函数发送发送消息,并使用Jtelebot库( https ://github.com/shibme/jtelebot )。我没有安装Javafx,而是使用Intellij默认Javafx库,该库一直在以前的项目中运行良好。该程序在包含自定义库之前正确打开,但是一旦我包括它,就会出现以下错误:

Error occurred during initialization of boot layer
java.lang.module.FindException: Module Telegram not found, required by my.group.project_telegram

有人对如何解决此问题有任何想法吗? 需要电报包含在 module-info.java 文件中。我还尝试从头开始重新重新重新完成该项目,但没有结果。这是我的一些项目文件:

build.gradle:

    plugins {
    id 'java'
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.10'
    id 'org.beryx.jlink' version '2.24.1'
}

group 'my.group'
version '1.0'

repositories {
    mavenCentral()
    maven {
        url "https://gitlab.com/api/v4/projects/<project-id>/packages/maven"
        credentials(HttpHeaderCredentials) {
            name = "Private-Token"
            value = gitLabPrivateToken // the variable resides in ~/.gradle/gradle.properties
        }
        authentication {
            header(HttpHeaderAuthentication)
        }
    }
}

ext {
    junitVersion = '5.8.2'
}

sourceCompatibility = '11'
targetCompatibility = '11'

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

application {
    mainModule = 'my.group.project_telegram'
    mainClass = 'my.group.project_telegram.HelloApplication'
}

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

dependencies {
    implementation('my.group:Telegram:1.0')
    implementation('org.controlsfx:controlsfx:11.1.1')
    implementation('com.dlsc.formsfx:formsfx-core:11.5.0') {
        exclude(group: 'org.openjfx')
    }

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

module-info.java:

module my.group.project_telegram {
    requires javafx.controls;
    requires javafx.fxml;

    requires org.controlsfx.controls;
    requires com.dlsc.formsfx;
    requires Telegram;

    opens my.group.project_telegram to javafx.fxml;
    exports my.group.project_telegram;
}

库:

package pkgTelegram;

import me.shib.java.lib.telegram.bot.service.TelegramBot;
import me.shib.java.lib.telegram.bot.types.*;

import java.io.IOException;

public class Telegram {

    public static void sendToTelegram(String apiToken, String chatId, String text) {
        // the function's code
    }
}

I am making a simple Telegram bot with my own custom library in JavaFX with OpenJDK 11. The project was built as a JavaFX project from IntelliJ IDEA, with Gradle as build system. The library consists of a single class (Telegram) with a single function to send a message, and uses the JTeleBot library (https://github.com/shibme/jtelebot). I do NOT have JavaFX installed, I am instead using the IntelliJ default JavaFX libraries, which has been working fine until now for previous projects. The program opens properly before including the custom library, but as soon as I include it, it gives the following error:

Error occurred during initialization of boot layer
java.lang.module.FindException: Module Telegram not found, required by my.group.project_telegram

Does anybody have any idea on how to fix this? require Telegram is included in the module-info.java file. I have also tried remaking the project from scratch, with no results. Here are some of my project files:

build.gradle:

    plugins {
    id 'java'
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.10'
    id 'org.beryx.jlink' version '2.24.1'
}

group 'my.group'
version '1.0'

repositories {
    mavenCentral()
    maven {
        url "https://gitlab.com/api/v4/projects/<project-id>/packages/maven"
        credentials(HttpHeaderCredentials) {
            name = "Private-Token"
            value = gitLabPrivateToken // the variable resides in ~/.gradle/gradle.properties
        }
        authentication {
            header(HttpHeaderAuthentication)
        }
    }
}

ext {
    junitVersion = '5.8.2'
}

sourceCompatibility = '11'
targetCompatibility = '11'

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

application {
    mainModule = 'my.group.project_telegram'
    mainClass = 'my.group.project_telegram.HelloApplication'
}

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

dependencies {
    implementation('my.group:Telegram:1.0')
    implementation('org.controlsfx:controlsfx:11.1.1')
    implementation('com.dlsc.formsfx:formsfx-core:11.5.0') {
        exclude(group: 'org.openjfx')
    }

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

module-info.java:

module my.group.project_telegram {
    requires javafx.controls;
    requires javafx.fxml;

    requires org.controlsfx.controls;
    requires com.dlsc.formsfx;
    requires Telegram;

    opens my.group.project_telegram to javafx.fxml;
    exports my.group.project_telegram;
}

The library:

package pkgTelegram;

import me.shib.java.lib.telegram.bot.service.TelegramBot;
import me.shib.java.lib.telegram.bot.types.*;

import java.io.IOException;

public class Telegram {

    public static void sendToTelegram(String apiToken, String chatId, String text) {
        // the function's code
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪也成诗 2025-01-28 15:39:30

经过一些反复试验,我最终提出了解决方案。正如@Jewelsea所说,问题来自我的图书馆没有模块的事实,而只是一个课程。在 this Answer 。我最终将以下代码添加到我的库的 build.grade

tasks.named('jar') {
    manifest {
        attributes('Automatic-Module-Name': 'my.group.Telegram')
    }
}

并修改了我的 module-info.java to:

module my.group.project_telegram {
    requires javafx.controls;
    requires javafx.fxml;

    requires org.controlsfx.controls;
    requires com.dlsc.formsfx;
    requires my.group.Telegram;

    opens my.group.project_telegram to javafx.fxml;
    exports my.group.project_telegram;

}

如果您无法修改库的文件,< href =“ https://stackoverflow.com/a/51918271”>此解决方案可能会起作用,但我自己没有尝试过。

After some trial and error, I ended up coming up with a solution. As @jewelsea said, the problem came from the fact that my library did not have a module, and instead is just a single class. The is explained further in the Gradle documentation and in this answer. I ended up adding the following code to my library's build.grade :

tasks.named('jar') {
    manifest {
        attributes('Automatic-Module-Name': 'my.group.Telegram')
    }
}

and modified my module-info.java to:

module my.group.project_telegram {
    requires javafx.controls;
    requires javafx.fxml;

    requires org.controlsfx.controls;
    requires com.dlsc.formsfx;
    requires my.group.Telegram;

    opens my.group.project_telegram to javafx.fxml;
    exports my.group.project_telegram;

}

If you can't modify your library's files, this solution might work, but I haven't tried it myself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文