从JavaFX1.8迁移到Java11+JavaFX和Maven
我一直在检查关于同一主题的其他问题,但我找不到具体的答案
我们有一个Java 8的老项目,这是一个为Windows和Mac构建的JavaFX应用程序。 我不是Java项目专家,开发该应用程序的公司也不复存在
由于苹果要求对应用程序进行公证,我们设法做到了。但他们最近改变了要求,现在失败了: -二进制文件使用比10.9 SDK旧的SDK -签名不包括安全的时间戳
这显示了大量的内部库(dylib)以及Contents/MacOS/App。 但这是我将来要面对的另一个问题。也许要遵循以下说明: Notarize existing Java application for MacOS Catalina
主要的问题是,我们试图用Java11+JavaFX编译这个项目,但现在我们在构建应用程序时遇到了问题。 注意:为了做到这一点,我们只需将JDK 8更改为JDK 11,并下载和附加JavaFX 11.0.2库
第一个问题是无法连接JavaFX库,但我们成功地通过运行/编辑配置完成了连接。 它解决了这个问题,但现在尝试从Intellij启动时,会显示以下错误:
Error:(18, 27) java: package netscape.javascript does not exist
Error:(9, 24) java: package org.w3c.dom.html does not exist
java cannot find symbol HTMLAnchorElement
这里有什么问题?这些包从Java11中删除了吗?Intellij允许我跟踪课程并查找内容
此外,我们使用Maven构建应用程序。我尝试过编辑配置等等,在构建、执行、部署中添加VM选项->;Maven->;但是当尝试打包应用程序时,它找不到JavaFX库:
--module-path /path/to/javafx-sdk-11.0.2/lib --add-modules=javafx.controls,javafx.fxml
我应该如何在Maven中加载JavaFX模块
所以现在我已经在Intellij中包含了JavaFX库,但无法从中启动,也无法在Maven中包含JavaFX
我很感激任何关于这方面的指导
编辑
我通过用新的导入替换导入,成功修复了netscape和HTMLanchoreElement错误:
import jdk.nashorn.api.scripting.JSObject;
Element anchorElement = (Element)event.getCurrentTarget();
通过在pom中添加这个,修复了maven中javafx的问题。xml文件:
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-base</artifactId>
<version>11</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>11</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>11</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-media</artifactId>
<version>11</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-swing</artifactId>
<version>11</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-web</artifactId>
<version>11</version>
</dependency>
我可以从Intellij启动该应用程序,但在尝试通过Maven打包时,我得到了以下信息:
Problem: failed to create task or type javafx:com.sun.javafx.tools.ant:deploy. Cause: The name is undefined
与此相关:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<configuration>
<target name="build-app">
<ant antfile="./javafx.build.xml"/>
</target>
</configuration>
<executions>
<execution>
<goals>
<goal>run</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
</plugin>
这是javafx。建筑xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="create-app" name="AppName" xmlns:fx="javafx:com.sun.javafx.tools.ant" xmlns:if="ant:if"
xmlns:unless="ant:unless">
<taskdef resource="com/sun/javafx/tools/ant/antlib.xml"
uri="javafx:com.sun.javafx.tools.ant"
classpath=".;${java.home}/../lib/ant-javafx.jar"/>
<condition property="onmac">
<os family="mac"/>
</condition>
<condition property="onwin">
<os family="windows"/>
</condition>
<target name="create-app" description="create jar file fx" if="os.name">
<echo message="Ant creates javafx app" />
<fx:deploy width="800" height="600" outdir="target/dist" outfile="App" nativeBundles="image" version="1.4.5">
<fx:info title="App Name">
<fx:icon href="installable/mac/logo.icns" kind="default" if:set="onmac" ></fx:icon>
<fx:icon href="src/main/resources/resources/logo_256.ico" kind="default" if:set="onwin"></fx:icon>
</fx:info>
<fx:application name="App Name" mainClass="recorder.DesktopRecorder" />
<fx:resources>
<fx:fileset dir="target/app-module-1.4.5-distribution" includes="*"/>
</fx:resources>
</fx:deploy>
<exec executable="./installable/mac/create-dmg.sh" if:set="onmac"/>
<exec executable="C:\Program Files (x86)\Inno Setup 5\ISCC" if:set="onwin">
<arg value="installable/windows/innosetup_prod.iss" />
</exec>
</target>
我包括ant javafx。jar文件,并更改为:
classpath="./ant-javafx.jar"
但在创建苹果“应用”文件时仍然失败:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.8:run (default) on project app-module: An Ant BuildException has occured: The following error occurred while executing this line:
[ERROR] /path/to/app/javafx.build.xml:24: Error: Bundler "Mac Application Image" (mac.app) failed to produce a bundle.
[ERROR] around Ant part ...... @ 4:38 in /path/to/app/target/antrun/build-build-app.xml
# 1 楼答案
自JDK 11以来,JavaFX已从JDK中删除。您可以按照以下步骤操作:
https://openjfx.io/openjfx-docs/
因为您的项目依赖于外部库,所以我建议使用非模块化形式的JavaFX项目。如果使用IntelliJ和Maven,建议使用:
JavaFX和IntelliJ->;带Maven的非模块化
或者
JavaFX和IntelliJ->;带渐变的非模块化
通过这种方式,您不再需要下载并附加JavaFXSDK或jmods;因为您使用的构建工具(Maven/Gradle)会自动为您的项目下载这些库和包