使用ant运行JavaFX8应用程序时出现异常

1bqhqjot  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(285)

我正在尝试使用ant编译和运行javafx项目。但是,当我尝试使用 ant run 启动应用程序时,出现以下错误:

run:
 [java] Exception in Application start method
 [java] java.lang.reflect.InvocationTargetException
 [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 [java]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 [java]     at java.lang.reflect.Method.invoke(Method.java:498)
 [java]     at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
 [java]     at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
 [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 [java]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
 [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
 [java]     at java.lang.reflect.Method.invoke(Method.java:498)
 [java]     at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:873)
 [java] Caused by: java.lang.RuntimeException: Exception in Application start method
 [java]     at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
 [java]     at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
 [java]     at java.lang.Thread.run(Thread.java:748)
 [java] Caused by: java.lang.IllegalStateException: Location is not set.
 [java]     at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
 [java]     at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
 [java]     at main.Main.start(Unknown Source)
 [java]     at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
 [java]     at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
 [java]     at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
 [java]     at java.security.AccessController.doPrivileged(Native Method)
 [java]     at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
 [java]     at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
 [java]     at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
 [java]     at com.sun.glass.ui.gtk.GtkApplication.lambda$null$5(GtkApplication.java:139)
 [java]     ... 1 more
 [java] Exception running application main.Main
 [java] Java Result: 1

从我在网上发现的情况来看,当找不到项目的.fxml文件时,就会出现这个错误。
这只发生在我尝试使用ant运行程序时。当我使用intellijide运行它时,一切都正常。在使用ide运行程序时,我甚至通过给它错误的.fxml资源url来重现这个错误。所以我猜ant就是找不到fxml文件,但是我在这方面迷失了方向,不知道如何让build.xml找到.fxml。
这是我的build.xml文件:

<project name="ija-app" basedir=".">
    <property name="src.dir"    value="src"/>
    <property name="build.dir"  value="build"/>
    <property name="jar.dir"    value="dest"/>
    <property name="lib.dir"    value="lib"/>

    <property name="main-class" value="main.Main"/>

    <path id="classpath">
        <pathelement location="${lib.dir}/jackson-annotations-2.9.0.jar"/>
        <pathelement location="${lib.dir}/jackson-core-2.9.9.jar"/>
        <pathelement location="${lib.dir}/jackson-databind-2.9.9.jar"/>
        <pathelement location="${lib.dir}/jackson-dataformat-yaml-2.9.9.jar"/>
        <pathelement location="${lib.dir}/snakeyaml-1.23.jar"/>
    </path>

    <target name="clean">
        <delete dir="${build.dir}/*"/>
        <delete file="${lib.dir}/*.jar"/>
    </target>

    <target name="compile" depends="clean">
        <exec executable="/bin/bash">
            <arg value="./lib/get-libs.sh"/>
        </exec>
        <javac srcdir="${src.dir}" destdir="${build.dir}">
               <classpath>
                   <path refid="classpath"/>
               </classpath>
        </javac>
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${build.dir}">
            <manifest>
                <attribute name="Main-Class" value="${main-class}"/>
            </manifest>
            <zipgroupfileset dir="${lib.dir}" includes="**/*.jar"/>
        </jar>
    </target>

    <target name="docs" depends="compile">
        <javadoc soudepath="${src.dir}" destdir="${doc.dir}">
            <classpath>
                <path refid="classpath"/>
            </classpath>
        </javadoc>
    </target>

    <target name="run">
        <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
    </target>
</project>

谢谢你给我关于如何运行它的建议。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题