【发布时间】:2014-09-09 13:22:44
【问题描述】:
我花了几个小时读了很多书,但找不到任何关于这个特定问题的线索。
我已经使用 Scenebuilder 2.0 和 Eclipse Luna 构建了一个基本的 JavaFX 应用程序。 它由一个主类、一个空 css 和一个原始控制器类组成,它现在执行一个按钮的操作。
应用程序被导出到一个 jar 中,应该在安装了 JRE 1.6 的 Linux/Suse 下运行,并且可能在 Windows 1.7 下运行。
在开发 PC 上,我安装了 JRE 1.8 和 JDK 1.7。该应用程序是针对 Linux 系统上的 JRE 1.6 导出的。
我制作了一个 Jar 并想通过键入来启动应用程序
java -jar <pathToMyApp/jarname.jar>
“线程“main”中的异常 java.lang.NoClassDefFoundError: javafx/application/Application”
所以我读到,如果你想用命令行启动它(Linux 或 Windows,没关系),你通常不能使用 rightlick export 等导出 jar。双击有效,命令行无效。所以我使用了 JavaFX 项目附带的 AntScript:build.fxbuild。我设置了名称参数等等点击“ant build.xml and run”
控制台错误:
(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 特定标签。
Ant 脚本:(正在运行)
<?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 是否存在版本冲突?
【问题讨论】:
标签: java eclipse ant jar javafx-8