从JavaFX 1.8迁移到Java 11 + JavaFX和Maven

3

我已经查看了其他关于同一主题的问题,但是我找不到适合我的具体答案。

我们有一个使用Java 8构建的旧项目,一个JavaFX应用程序,我们为Windows和Mac构建。我不是Java项目的专家,开发该应用程序的公司也不再可用。

由于苹果要求对应用程序进行认证,我们设法完成了这个过程。但是他们最近更改了要求,现在失败了: - 二进制文件使用的SDK早于10.9 SDK - 签名不包括安全时间戳

这适用于大量内部库(dylib)以及Contents / MacOS / App。但这是我将来要面临的另一个问题。也许遵循以下说明: Notarize existing Java application for MacOS Catalina

主要问题是我们尝试使用Java 11 + JavaFX编译项目,但现在我们在构建应用程序时遇到了问题。 注意:为了做到这一点,我们只是从JDK 8更改为JDK 11,并下载并附加了JavaFX 11.0.2库。

第一个问题是无法附加JavaFX库,但是我们设法从Run/Edit configurations中进行了操作。 它解决了问题,但是现在尝试从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

这里有什么问题?Java 11已经删除了这些包吗?Intellij允许我跟踪类并查找内容。

此外,我们使用Maven构建应用程序。我尝试编辑配置等,在Build,Execution,Deployment -> Maven -> Runner中添加VM选项,但是在尝试打包应用程序时,它无法找到JavaFX库:

--module-path /path/to/javafx-sdk-11.0.2/lib --add-modules=javafx.controls,javafx.fxml

我该如何在Maven中加载JavaFX模块?

因此,此时我已在Intellij中包含了JavaFX库,但无法从其中启动,并且无法在Maven中包含JavaFX。

我感谢您对此的任何指导。

编辑

我通过替换导入的新导入来修复了netscape和HTMLAnchorElement错误:

import jdk.nashorn.api.scripting.JSObject;


Element anchorElement = (Element)event.getCurrentTarget();

通过在pom.xml文件中添加以下内容,解决了javafx在maven中的问题:

    <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打包时,出现以下错误:

问题: 未能创建任务或类型javafx:com.sun.javafx.tools.ant:deploy。 原因: 名称未定义

与此相关的是:

                <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.build.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"

但是在创建苹果"app"文件时仍然失败了:

[ERROR] 无法执行目标 org.apache.maven.plugins:maven-antrun-plugin:1.8:run (default) 在项目 app-module 上:发生了 Ant BuildException 错误:执行此行时发生以下错误:

[ERROR] /path/to/app/javafx.build.xml:24: 错误:Bundler "Mac Application Image" (mac.app) 未能生成捆绑包。

[ERROR] around Ant 部分...... @ 4:38 in /path/to/app/target/antrun/build-build-app.xml

1个回答

4

JDK 11版本以后,JavaFX已被从JDK中删除。你可以按照以下步骤进行:

https://openjfx.io/openjfx-docs/

由于你的项目依赖于外部库,建议使用JavaFX项目的非模块化形式。如果你使用IntelliJ和Maven,建议使用以下方式:

JavaFX和IntelliJ -> 使用Maven的非模块化形式

或者

JavaFX和IntelliJ -> 使用Gradle的非模块化形式

这样,你就不需要再下载和附加JavaFX SDK或jmods了;因为你使用的构建工具(Maven / Gradle)将自动为你的项目下载这些库和包。


谢谢Amir! 问题不在于添加JavaFX库。 我将检查如何将其包含为非模块形式,但我现在的优先事项是能够生成DMG文件。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接