繁体   English   中英

JavaFX应用程序未从命令行运行/如何正确构建和导出?

[英]JavaFX application not running from command line / how to build and export correctly?

我花了好几个小时阅读大量书籍,但是找不到这个特定问题的线索。

我已经使用Scenebuilder 2.0和Eclipse Luna构建了一个基本的JavaFX应用程序。 它由一个主类,一个空的CSS和一个原始控制器类组成,该类现在对一个按钮执行操作。

该应用程序已导出到jar中,并且应在安装JRE 1.6以及Windows 1.7的Linux / Suse下运行。

在开发PC上,我安装了JRE 1.8和JDK 1.7。 该应用程序已在Linunx系统上导出为JRE 1.6。

我做了一个Jar,并想通过键入java -jar <pathToMyApp/jarname.jar>来启动应用程序。

“线程“主”中的异常java.lang.NoClassDefFoundError:javafx / application / Application”

因此,我读到,如果您想使用命令行启动它(Linux或Windows,没关系),通常就无法通过rightlick导出来导出jar。 双击有效,命令行无效。 因此,我使用了JavaFX项目随附的AntScript:build.fxbuild。 我设置名称参数,依此类推,然后单击“ ant build.xml并运行”

控制台错误:

(Only one entry, actually there are 52):
 [javac] warning: C:\Program Files (x86)\Java\jre8\lib\ext\jfxrt.jar(javafx/application/Application.class): major version 52 is newer than 51, the highest major version supported by this compiler.
 [javac]   It is recommended that the compiler be upgraded.

BUILD FAILED
****\****\****\JavaFXTest\build\build.xml:84: Problem: failed to create task or type javafx:com.sun.javafx.tools.ant:resources
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
No types or tasks have been defined in this namespace yet

我做了很多阅读,只发现您应该将JAVA_HOME路径设置为JDK而不是JRE。 这导致JavaFX无法正常工作,无法识别JavaFX特定的标签。

蚂蚁脚本:(正在运行)

<?xml version="1.0" encoding="UTF-8"?>
<project name="JavaFXTest" default="do-deploy" basedir="."  xmlns:fx="javafx:com.sun.javafx.tools.ant">
<target name="init-fx-tasks">
    <path id="fxant">
        <filelist>
            <file name="${java.home}\..\lib\ant-javafx.jar"/>
            <file name="${java.home}\lib\jfxrt.jar"/>
        </filelist>
    </path>

    <taskdef resource="com/sun/javafx/tools/ant/antlib.xml"      
        uri="javafx:com.sun.javafx.tools.ant"
        classpathref="fxant"/>
</target>
<target name="setup-staging-area">
    <delete dir="externalLibs" />
    <delete dir="project" />
    <delete dir="projectRefs" />

    <mkdir dir="externalLibs" />

    <copy todir="externalLibs">
        <fileset dir="C:\Program Files (x86)\Java\jre8\lib\ext">
            <filename name="jfxrt.jar"/>    
        </fileset>
    </copy>

    <mkdir dir="project" />
    <copy todir="project">
        <fileset dir="D:\Entwicklung\workspace\JavaFXTest">
            <include name="src/**" />
        </fileset>
    </copy>
    <copy todir="project">
        <fileset dir="D:\Entwicklung\workspace\JavaFXTest">
            <include name="utils4j/**" />
        </fileset>
    </copy>

    <mkdir dir="projectRefs" />
</target>
<target name='do-compile'>
    <delete dir="build" />
    <mkdir dir="build/src" />
    <mkdir dir="build/libs" />
    <mkdir dir="build/classes" />

    <!-- Copy project-libs references -->
    <copy todir="build/libs">
        <fileset dir="externalLibs">
            <include name="jfxrt.jar"/>
        </fileset>
    </copy>

    <!-- Copy project references -->

    <!-- Copy project sources itself -->
    <copy todir="build/src">
        <fileset dir="project/utils4j">
            <include name="**/*"/>
        </fileset>
    </copy>
    <copy todir="build/src">
        <fileset dir="project/src">
            <include name="**/*"/>
        </fileset>
    </copy>

    <javac includeantruntime="false" source="1.6" target="1.6" srcdir="build/src" destdir="build/classes" encoding="Cp1252">
        <classpath>
            <fileset dir="build/libs">
                <include name="*"/>
            </fileset>
        </classpath>
    </javac>

    <!-- Copy over none Java-Files -->
    <copy todir="build/classes">
    <fileset dir="project/utils4j">
        <exclude name="**/*.java"/>
    </fileset>
    <fileset dir="project/src">
        <exclude name="**/*.java"/>
    </fileset>
    </copy>


</target>
<target name="do-deploy" depends="setup-staging-area, do-compile, init-fx-tasks">
    <delete file="dist"/>
    <delete file="deploy" />

    <mkdir dir="dist" />
    <mkdir dir="dist/libs" />

    <copy todir="dist/libs">
        <fileset dir="externalLibs">
            <include name="*" />
        </fileset>
    </copy>


    <fx:resources id="appRes">
        <fx:fileset dir="dist" includes="JavaFXTest.jar"/>
        <fx:fileset dir="dist" includes="libs/*"/>
    </fx:resources> 

    <fx:application id="fxApplication"
        name="AMAN_JAVAFX"
        mainClass="application.Main"
        toolkit="fx"
    />

    <mkdir dir="build/classes/META-INF" />



    <fx:jar destfile="dist/JavaFXTest.jar">
        <fx:application refid="fxApplication"/>
        <fileset dir="build/classes">
        </fileset>
        <fx:resources refid="appRes"/>

        <manifest>
            <attribute name="Implementation-Vendor" value="me"/>
            <attribute name="Implementation-Title" value="AMAN_JAVAFX"/>
            <attribute name="Implementation-Version" value="1.0"/>
            <attribute name="JavaFX-Feature-Proxy" value="None"/>
        </manifest>
    </fx:jar>


    <mkdir dir="deploy" />
    <!-- Need to use ${basedir} because somehow the ant task is calculating the directory differently -->
    <fx:deploy
        embedJNLP="false"
        extension="false"
        includeDT="false"
        offlineAllowed="true"
        outdir="${basedir}/deploy"
        outfile="JavaFXTest" nativeBundles="none"
        updatemode="background" >

        <fx:info title="JavaFXTest" vendor="me"/>
        <fx:application refId="fxApplication"/>
        <fx:resources refid="appRes"/>
    </fx:deploy>


</target>
</project>

问题:如何正确出口? 设置JAVA_HOME是什么? JavaFX的启动命令是否错误? JRE 1.8和JDK 1.7之间是否存在版本冲突?

首先:“线程“ main”中的异常java.lang.NoClassDefFoundError:javafx / application / Application”。 这是因为JavaFX库在类路径中不可用。 这些库在JRE 6中根本不存在。它们包含在JRE 7中(从版本7u6开始),但默认情况下也不在类路径中。 我认为,如果您针对Java 6进行编译并且在jar中包含jfxrt.jar库(来自JDK 7),则可以使您的应用程序在JRE 6上运行。

请注意,即使您的目标是Java 7+,您也应该在JAR中包含jfxrt.jar库,因为JavaFX版本在JRE的所有次要版本中都不相同(并且7u6之前的版本甚至不包括它)。

更好的解决方案可能是打包一个自包含应用程序,嵌入其自己的JRE,这样您就不必依赖目标计算机上安装的JRE。 这样,您就可以使用Java 8和JavaFX 8的所有超酷新功能使用JDK 8进行开发,无论目标计算机上安装了JRE如何,都可以使用dit。 但是,缺点是程序包较大,并且需要针对不同的操作系统使用特定的程序包。 一个很好的起点是Oracle文档中的此页面

否则,如果您想获得有关蚂蚁构建脚本的更多帮助,则可能需要发布实际的脚本。 显而易见的一件事是,它试图在JRE 8安装中使用JavaFX库(它是​​为Java 8编译的,这是类文件格式的主要版本52),而试图为Java 7(主要版本51)进行编译时-该库不兼容。

编辑:经过进一步的挖掘,我认为如果要部署到JRE 6,则需要下载JDK 6JavaFX SDK -JDK 7附带的JavaFX版本可能无法在Java 6上运行。请不要因为JDK 6的JavaFX仅适用于Windows(并且不再受支持),所以我认为您将无法仅使用JRE 6部署到Linux机器(当然,包含独立的应用程序) 。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM