簡體   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