首页 > 解决方案 > 如何用 module-info.java 文件替换 VM 参数

问题描述

我刚刚开始了一个基于AdoptOpenJDK 11and的新项目OpenJavaFX 11

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

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

之前的 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

Duplicate requires entry: javafx.fxml

我该如何解决这个问题?

标签: javaeclipsejavafxruntime-errorjvm-arguments

解决方案


选项 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)。


推荐阅读