gpt4 book ai didi

java - 如何用 module-info.java 文件替换 VM 参数

转载 作者:行者123 更新时间:2023-12-02 09:13:27 28 4
gpt4 key购买 nike

我刚刚启动了一个基于 AdoptOpenJDK 11OpenJavaFX 11 的新项目。

之前,在我的开发过程中,我使用了 VM 参数,当您将项目传递给其他人时,这并不总是实用。

所以我开始用 module-info.java 文件替换我的 VM 参数。

之前的VM参数:

--module-path ${PATH_TO_FX} –add-modules javafx.controls,javafx.fxml 

--add-opens
javafx.base/com.sun.javafx.runtime=ALL-UNNAMED
--add-opens
javafx.controls/com.sun.javafx.scene.control.behavior=ALL-UNNAMED
--add-opens
javafx.controls/com.sun.javafx.scene.control=ALL-UNNAMED
--add-opens
javafx.base/com.sun.javafx.binding=ALL-UNNAMED
--add-opens
javafx.base/com.sun.javafx.event=ALL-UNNAMED
--add-opens
javafx.graphics/com.sun.javafx.stage=ALL-UNNAMED
--add-opens
javafx.graphics/com.sun.javafx.scene=ALL-UNNAMED

module-info.java 现在文件:

module hellofx {

requires javafx.controls;
requires javafx.fxml;
requires transitive javafx.graphics;

opens org.openjfx to javafx.fxml;
exports org.openjfx;

}

这就是我遇到问题的地方,我设法替换了标签 --module path 但我找不到标签 --add-open 的解决方案。

有人可以帮助我吗?

编辑 12/09:

现在我在 module-info.java

中的这两行出现了一个新错误

重复需要输入:javafx.fxml

我该如何解决这个问题?

最佳答案

选项 A 快速且干净:

改变


module hellofx {

requires javafx.controls;
requires javafx.fxml;
requires transitive javafx.graphics;

//wrong
opens org.openjfx to javafx.fxml;

//right
opens com.your.package to javafx.fxml, javafx.controls;

exports org.openjfx;

}

选项 B(因为用 javaFX 构建 Jar 似乎很奇怪)

您也可以使用 Gradle 来完成此操作(因为这样您就可以轻松构建 JavaFX jar,而不会出现太多问题)

您应该添加 gradle fx 插件,并定义编译参数。

当您使用 Gradle 时,您也不需要手动下载 openJavaFX 库。

您的 Gradle 文件应如下所示




buildscript {
dependencies {
classpath group: 'de.dynamicfiles.projects.gradle.plugins', name: 'javafx-gradle-plugin', version: '8.7.0'
classpath 'org.openjfx:javafx:11'

}
repositories {
mavenLocal()
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}

}
}

plugins {
id 'eclipse'
id 'java'
id 'java-library'
id 'application'
// This is very Important!
id 'org.openjfx.javafxplugin' version '0.0.8'
id 'org.beryx.jlink' version '2.12.0'
}

sourceCompatibility = 11
targetCompatibility = 11
repositories {
jcenter()
mavenCentral()
}



mainClassName="com.your.MainLauncher"



// Use this for the Runtime, to run with the required Modules

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


sourceSets {
main.java.srcDir "src/main/java"
main.resources.srcDir "src/main/resources"
}

// Your dependecies
dependencies {

compile group: 'org.openjfx', name: 'javafx', version: '11.0.2'
// JAVA FX dependecy. Important!
// Add your other dependecies


}

// to build the jar
jar {
manifest {
attributes(
'Main-Class': 'com.your.MainLauncher'
)

}
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}

// to "include compile arguments " that replace your VM arguments
compileJava {
doFirst {
options.compilerArgs = [
'--module-path', classpath.asPath,
'--add-modules', 'javafx.controls,javafx.fxml,javafx.graphics'
]
println options.compilerArgs
}

}


您可以使用 gradle 运行它,也可以使用 eclipse 运行它。当您构建 jar 时,也不需要 VM 参数。我希望这会对您有所帮助。

如果您安装了 Gradle 支持插件,这也会耗尽您的 IDE(intelliJ/Eclipse)。

关于java - 如何用 module-info.java 文件替换 VM 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59214300/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com