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

huangapple go评论80阅读模式
英文:

Migrating from JavaFX 1.8 to Java 11 + JavaFX and Maven

问题

以下是翻译好的内容:

我已经检查了关于相同主题的其他问题,但我找不到适用于我的具体答案。

我们有一个旧的Java 8项目,一个用于Windows和Mac的JavaFX应用程序。
我不是Java项目的专家,开发该应用程序的公司已不再提供支持。

由于Apple要求对应用程序进行公证,我们设法做到了。但他们最近改变了要求,现在出现了问题:

  • 该二进制文件使用比10.9 SDK更旧的SDK。
  • 该签名不包括安全时间戳。

这适用于大量内部库(dylib)以及Contents/MacOS/App。
但这是我将来要面临的另一个问题。也许可以按照以下说明进行操作:
https://stackoverflow.com/questions/58548736/notarize-existing-java-application-for-macos-catalina

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

第一个问题是无法附加JavaFX库,但我们设法从运行/编辑配置中完成了。
这解决了问题,但现在在尝试从Intellij启动时,出现了以下错误:

> 错误:(18, 27)java: 包netscape.javascript不存在

> 错误:(9, 24)java: 包org.w3c.dom.html不存在

> java找不到符号HTMLAnchorElement

这里的问题是什么?这些包在Java 11中被移除了吗?Intellij允许我跟踪类并查找内容。

另外,我们使用Maven构建应用程序。我尝试过编辑配置等等,在“构建、执行、部署” -> “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文件中添加以下内容解决了Maven中的javafx问题:

    <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
英文:

I have been checking other questions about the same topic but i couldn't find the specific answers for me.

We have an old project in Java 8, a JavaFX application that we build for Windows and Mac.
I am not an expert of Java projects and the company which develop the app is no longer available.

As Apple requires notarizing the app, we managed to do it. But they recently changed the requirements and now it fails:

  • The binary uses an SDK older than the 10.9 SDK
  • The signature does not include a secure timestamp

This is shown for tons of internal libraries (dylib) as well as the Contents/MacOS/App.
But this is another problem i will face it in the future. Maybe following these instructions:
https://stackoverflow.com/questions/58548736/notarize-existing-java-application-for-macos-catalina

The main problem is that we tried to compile the project with Java 11 + JavaFX but now we have problems building the app.
Note: in order to do this we simply changed from JDK 8 to JDK 11 as well as downloading and attaching the JavaFX 11.0.2 library.

The first problem was not being able to attach the JavaFX library but we managed to do it from Run/Edit configurations.
It fixes the issue but now when trying to launch from Intellij it shows errors like:

> 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

What's the problem here? Have those packages removed from Java 11? Intellij allows me to follow the class and find the content.

In addition, we use Maven for building the app. I have tried editing the configuration and so on, adding the VM options in Build, Execution, Deployment -> Maven -> Runner but when trying to package the app, it doesn't find the JavaFX library:

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

How am i supposed to load the JavaFX module in Maven?

So at this point i have included the JavaFX library in Intellij but unable to launch from it, and unable to include the JavaFX in Maven.

I appreciate any guidance on this.

EDIT:

I managed to fix the netscape and HTMLAnchorElement errors by replacing the imports by new ones:

import jdk.nashorn.api.scripting.JSObject;


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

And fixed the issue with javafx in maven by adding this in pom.xml file:

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

I can launch the app from Intellij but when trying to package it via Maven, I got this:

> Problem: failed to create task or type javafx:com.sun.javafx.tools.ant:deploy.
> Cause: The name is undefined

Related to this:

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

And this is the 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>

</project>

I included the ant-javafx.jar file in the project and changed to this:

classpath=&quot;./ant-javafx.jar&quot;

But it still failed when creating the Apple "app" file:

> [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 ...<ant antfile="./javafx.build.xml"/>... @ 4:38 in /path/to/app/target/antrun/build-build-app.xml

答案1

得分: 3

JavaFX自JDK 11起已从JDK中移除。您可以按照以下步骤操作:

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

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

JavaFX与IntelliJ -> 使用Maven的非模块化

或者

JavaFX与IntelliJ -> 使用Gradle的非模块化

通过这种方式,您不再需要下载和附加JavaFX SDK或jmods;因为您使用的构建工具(Maven/Gradle)将自动为您的项目下载这些库和包。

英文:

JavaFX has been removed from JDK since JDK 11. You can follow the steps here:

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

Because your project depends on external libraries I recommend using the Non-modular forms of JavaFX projects. If you use IntelliJ and Maven it is recommended using:

JavaFX And IntelliJ -> Non-modular with Maven

or

JavaFX And IntelliJ -> Non-modular with Gradle

In this way, you do not need to download and attach JavaFX SDK or jmods anymore; Because the build tool you use (Maven/Gradle) will automatically download those libraries and packages for your project.

huangapple
  • 本文由 发表于 2020年5月5日 15:20:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/61607795.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定