简体   繁体   中英

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

I did a lot of reading for hours, but can´t find any thread for this SPECIFIC problem.

I have build a basic JavaFX application with Scenebuilder 2.0 and Eclipse Luna. It consists of a main class, an empty css and a primitive controller class, that does the action for one button right now.

The application was exported to a jar and should run under Linux/Suse with JRE 1.6 installed and maybe Windows 1.7.

On the development PC I have JRE 1.8 installed and JDK 1.7. The application was exported for JRE 1.6 on the Linunx System.

I made a Jar and wanted to start the application by typing java -jar <pathToMyApp/jarname.jar>

"Exception in thread "main" java.lang.NoClassDefFoundError: javafx/application/Application"

So I read, that you can´t normally export a jar with rightlick export and so on, if you want to start it with the commandline (Linux or Windows, doesn´t matter). Double-clicking works, commandline doesn´t. So I used the AntScript that comes with a JavaFX project: build.fxbuild. I set the name parameters and so on and clicked "ant build.xml and run"

Error in Console:

(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

I did a lot of reading and only found that you should set the JAVA_HOME path to your JDK instead of the JRE. This leads to JavaFX not working correctly, no JavaFX specific tags are recognised.

Ant script: (Now running)

<?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>

Questions: How to export properly? What to set JAVA_HOME? Is my starting command wrong for JavaFX? Is there a version conflict between JRE 1.8 and JDK 1.7?

First of all: "Exception in thread "main" java.lang.NoClassDefFoundError: javafx/application/Application". This is because the JavaFX libraries are not available in the classpath. Those libraries don't exist at all in the JRE 6. They are included in the JRE 7 (starting with version 7u6), but are not on the classpath by default either. I think it's possible to get your application to run on JRE 6 if you compile for Java 6 and if you include the jfxrt.jar library (from your JDK 7) in your jar.

Note that even if you target Java 7+, you should still include the jfxrt.jar library in your JAR, as the JavaFX version isn't the same in all minor revisions of the JRE (and versions older than 7u6 don't even include it).

A better solution is probably to package a Self-Contained Application, embedding its own JRE, so that you don't rely on the JRE installed on the target machine. That way, you can develop with a JDK 8, using all the cool new features of Java 8 and JavaFX 8, an dit will work regardless of the JRE installed on the target machine. The drawbacks, however, are bigger packages, and the need to have specific packages for different operating systems. A good starting point is this page in Oracle's documentation.

Otherwise, if you want more help with your ant build script, you will probably need to post the actual script. One thing that's apparent is that it's trying to use the JavaFX library from your JRE 8 install (it's compiled for Java 8, which is major version 52 of the class file format) while trying to compile for Java 7 (major version 51) - so the library isn't compatible.

Edit: After some more digging, I think that if you want to deploy to the JRE 6, you will need to download the JavaFX SDK for JDK 6 - the version of JavaFX that comes with JDK 7 probably won't work on Java 6. Please not that JavaFX for JDK 6 is only available for Windows (and is no longer supported), so I don't think you will be able to deploy to Linux machines with only a JRE 6 (self-contained applications excepted, of course).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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