当前位置: 首页 > 知识库问答 >
问题:

Java11/JavaFX和Maven不会在NetBeans IDE 9之外运行

管景天
2023-03-14

根据JavaFX部署库,在活动JDK中找不到

我使用了JoséPereda Maven方法,它在NetBeans中运行得很好,但只要我尝试使用“java-jar-mavenproject1-1.0-SNAPSHOT-jar-with-dependencies.jar”在外部运行它,我就会得到以下错误:“错误:缺少JavaFX运行时组件,运行此应用程序需要这些组件”
“md.mavenproject1.MainApp”只是一个临时文件在我想办法解决这个问题的时候说出你的名字
我的pom文件看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>md</groupId>
    <artifactId>mavenproject1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>mavenproject1</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <mainClass>md.mavenproject1.MainApp</mainClass>
    </properties>

    <organization>
        <!-- Used as the 'Vendor' for JNLP generation -->
        <name>Your Organisation</name>
    </organization>

    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>11-ea+25</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>11-ea+25</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <release>11</release>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.ow2.asm</groupId>
                        <artifactId>asm</artifactId>
                        <version>6.1.1</version>
                        <!--  Use newer version of ASM  -->
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>md.mavenproject1.MainApp</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>md.mavenproject1.MainApp</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin> 
        </plugins>
    </build>
</project>

还有我的行动。xml文件看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<actions>
        <action>
            <actionName>run</actionName>
            <goals>
                <goal>clean</goal>
                <goal>package</goal>
                <goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:java</goal>
            </goals>
            <properties>
                <runfx.args>-jar "${project.build.directory}/${project.build.finalName}.jar"</runfx.args>
            </properties>
        </action>
        <action>
            <actionName>debug</actionName>
            <goals>
                <goal>clean</goal>
                <goal>package</goal>
                <goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:java</goal>
            </goals>
            <properties>
                <runfx.args>-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} -Dglass.disableGrab=true -jar "${project.build.directory}/${project.build.finalName}.jar"</runfx.args>
                <jpda.listen>true</jpda.listen>
            </properties>
        </action>        
    </actions>

你能给我的任何帮助都将是巨大的帮助,因为我正在考虑用JavaFX的变化将我的高中编程孩子从ant切换到maven,但他们希望能够将这些程序带回家使用。我正在我的系统上运行OpenJDK 11
**编辑**
我将运行程序的行更改为:

java --module-path c:\javafx-sdk-11\lib --add-modules javafx.controls -jar mavenproject1-1.0-SNAPSHOT-jar-with-dependencies.jar

现在我得到了:

Exception in Application start method
java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
        at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module @0x11a24f71) cannot access class com.sun.javafx.util.Utils (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.util to unnamed module @0x11a24f71
        at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
        at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
        at md.test33.MainApp.start(MainApp.java:15)
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
        at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
        at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
        ... 1 more
Exception running application md.mavenproject1.MainApp

我认为使用maven的依赖项不需要使用本地jdk。任何人能给予的任何帮助都会非常有帮助。如果我去掉依赖项并使用oraclejava10jdk,那么程序将在netbeans之外运行,而使用java11则只在netbeans内部运行。请帮忙

 <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>md.mavenproject1.MainApp</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin> 

取而代之的是使用:

  <!--  Used to make dependencys needed to run the program -->  
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- makes the JAR file to run the program outside -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>           
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>${mainClass}</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

我现在可以将启动行更改为使用我添加的附加JavaFX库,而不必使用我下载的SDK。

java --module-path lib --add-modules javafx.controls --add-exports=javafx.graphics/com.sun.javafx.util=ALL-UNNAMED --add-exports=javafx.base/com.sun.javafx.reflect=ALL-UNNAMED --add-exports=javafx.base/com.sun.javafx.beans=ALL-UNNAMED --add-exports=javafx.graphics/com.sun.glass.utils=ALL-UNNAMED --add-exports=javafx.graphics/com.sun.javafx.tk=ALL-UNNAMED -jar mavenproject1-1.0-SNAPSHOT.jar

我想让它包含jre,而不是使用本地jre。如果有人知道这会有多好。

共有2个答案

金泉
2023-03-14

在IntelliJ IDEA中使用openjdk 11和openjfx 11执行默认的Hello world javafx项目时,我也遇到了类似的问题。工作是在项目配置中使用提供的CLI。

但在这种情况下,解决方案是一种黑客行为,而不是正确的配置修复。我的问题是我没有任何模块信息。java初始化适当的java平台模块。因此IntelliJ模块实际上是未命名的模块。创建适当的模块描述符后,项目不再是未命名的平台模块。

我得到的下一个问题是关于使用外部java平台模块作为jar文件(具体来说是openjfx libs)。这需要通过在项目结构设置中添加库来解决。

然后,它将在没有任何附加CLI参数的情况下工作。

欧阳翔
2023-03-14

解决此问题的一种方法是将以下参数附加到命令行

--add-exports=javafx.graphics/com.sun.javafx.util=ALL-UNNAMED
 类似资料:
  • 我刚刚在Java11以IntelliJ Maven项目的形式开发了我的第一个JavaFX应用程序,并达到了它按预期工作的地步。到目前为止,我只使用Maven来处理依赖关系,使用IntelliJ来编译和运行我的项目。 然后我继续尝试以JAR文件的形式打包项目,发现由于Java11不再包含JavaFX,JAR生成变得更加困难(是的,我知道,我应该早点检查这个)。在阅读JavaFX 11的文档时,我决定

  • 我读了很多关于JavaFX GUI模型、和线程的文章,但我仍然不知道如何正确地实现这一点。我有一个JavaFX GUI,它在一个按钮上单击,执行一个进程,更新一个进度条和标签。这在线程和平台上运行得很好,但我不得不将其改为一个观察者模型。 我在一个单例模型中调用一个进度跟踪器,它由执行流程的类更新,并且是可观察的。我还实现了一个观察者,它应该更新两个UI元素。 带有按钮事件的GUI控制器 进度观察

  • 我在使用gradle:installDist分发JavaFX应用程序时遇到问题。我把这个问题简化为一个基本的例子。 我的应用程序有以下设置: OpenJDK 11 build.gradle: 代码: persistence.xml: 当我在IDE中运行它时,一切都很好。但是,当我使用gradle:installDist分发应用程序并尝试运行startscript时,会出现以下异常: 然而,当我删除

  • 当Java11排除了JavaFX作为最新版本的一部分时,我得到了以下错误。 那么如何在Java11中将JavaFX添加到Eclipse中呢?谢了。

  • 在此代码中: } 我不明白为什么它在initialise()中有一个值,但在start中却是空的。调试时,很明显,initialize()是由FXMLLOader从start()内部调用的 我本来打算发布fxml,但它似乎不起作用,正如预览中没有显示的那样。无论如何,它是一个真正的基本文件,一个BordePane和一个工具栏。 有线索吗?

  • 我们正在用Adopt JDK11和IntelliJ使用Gradle开发一个JavaFX11应用程序。最后,我们需要一个用于windows的EXE文件(该应用程序仅为windows设计)。在第一次尝试中,我们也使用了gradle中的launch4J,由于以下问题,我们也可以使用BAT文件,我们可以迁移到exe文件。 所以我们的主要目标和问题是,如何才能创建一个可执行的jar文件。 我们进行了几次尝试