gradle 如何以及在哪里可以在M1 Mac上下载JavaFX?

egdjgwm8  于 2023-10-19  发布在  Mac
关注(0)|答案(1)|浏览(108)

我一直在尝试为一个项目下载JavaFX,当我尝试运行gradle时,我的笔记本电脑就是没有它。
我可以很好地构建gradle,但是当我尝试gradle run时,我得到了这样的消息:

> Configure project :
Project : => no module-info.java found

> Task :run FAILED
Loading library prism_es2 from resource failed: java.lang.UnsatisfiedLinkError: /Users/claretian/.openjfx/cache/20+19/aarch64/libprism_es2.dylib: dlopen(/Users/claretian/.openjfx/cache/20+19/aarch64/libprism_es2.dylib, 0x0001): tried: '/Users/claretian/.openjfx/cache/20+19/aarch64/libprism_es2.dylib' (mach-o file, but is an incompatible architecture (have (x86_64), need (arm64e)))
java.lang.UnsatisfiedLinkError: /Users/claretian/.openjfx/cache/20+19/aarch64/libprism_es2.dylib: dlopen(/Users/claretian/.openjfx/cache/20+19/aarch64/libprism_es2.dylib, 0x0001): tried: '/Users/claretian/.openjfx/cache/20+19/aarch64/libprism_es2.dylib' (mach-o file, but is an incompatible architecture (have (x86_64), need (arm64e)))
        at java.base/jdk.internal.loader.NativeLibraries.load(Native Method)
        at java.base/jdk.internal.loader.NativeLibraries$NativeLibraryImpl.open(NativeLibraries.java:331)
        at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:197)
        at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:139)
        at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2404)
        at java.base/java.lang.Runtime.load0(Runtime.java:817)
        at java.base/java.lang.System.load(System.java:2015)
        at javafx.graphics@20/com.sun.glass.utils.NativeLibLoader.installLibraryFromResource(NativeLibLoader.java:217)
        at javafx.graphics@20/com.sun.glass.utils.NativeLibLoader.loadLibraryFromResource(NativeLibLoader.java:197)
        at javafx.graphics@20/com.sun.glass.utils.NativeLibLoader.loadLibraryInternal(NativeLibLoader.java:138)
        at javafx.graphics@20/com.sun.glass.utils.NativeLibLoader.loadLibrary(NativeLibLoader.java:54)
        at javafx.graphics@20/com.sun.prism.es2.ES2Pipeline.lambda$static$0(ES2Pipeline.java:63)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:319)
        at javafx.graphics@20/com.sun.prism.es2.ES2Pipeline.<clinit>(ES2Pipeline.java:52)
        at java.base/java.lang.Class.forName0(Native Method)
        at java.base/java.lang.Class.forName(Class.java:391)
        at java.base/java.lang.Class.forName(Class.java:382)
        at javafx.graphics@20/com.sun.prism.GraphicsPipeline.createPipeline(GraphicsPipeline.java:218)
        at javafx.graphics@20/com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.init(QuantumRenderer.java:92)
        at javafx.graphics@20/com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run(QuantumRenderer.java:125)
        at java.base/java.lang.Thread.run(Thread.java:1623)
Loading library prism_sw from resource failed: java.lang.UnsatisfiedLinkError: /Users/claretian/.openjfx/cache/20+19/aarch64/libprism_sw.dylib: dlopen(/Users/claretian/.openjfx/cache/20+19/aarch64/libprism_sw.dylib, 0x0001): tried: '/Users/claretian/.openjfx/cache/20+19/aarch64/libprism_sw.dylib' (mach-o file, but is an incompatible architecture (have (x86_64), need (arm64e)))
java.lang.UnsatisfiedLinkError: /Users/claretian/.openjfx/cache/20+19/aarch64/libprism_sw.dylib: dlopen(/Users/claretian/.openjfx/cache/20+19/aarch64/libprism_sw.dylib, 0x0001): tried: '/Users/claretian/.openjfx/cache/20+19/aarch64/libprism_sw.dylib' (mach-o file, but is an incompatible architecture (have (x86_64), need (arm64e)))
        at java.base/jdk.internal.loader.NativeLibraries.load(Native Method)
        at java.base/jdk.internal.loader.NativeLibraries$NativeLibraryImpl.open(NativeLibraries.java:331)
        at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:197)
        at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:139)
        at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2404)
        at java.base/java.lang.Runtime.load0(Runtime.java:817)
        at java.base/java.lang.System.load(System.java:2015)
        at javafx.graphics@20/com.sun.glass.utils.NativeLibLoader.installLibraryFromResource(NativeLibLoader.java:217)
        at javafx.graphics@20/com.sun.glass.utils.NativeLibLoader.loadLibraryFromResource(NativeLibLoader.java:197)
        at javafx.graphics@20/com.sun.glass.utils.NativeLibLoader.loadLibraryInternal(NativeLibLoader.java:138)
        at javafx.graphics@20/com.sun.glass.utils.NativeLibLoader.loadLibrary(NativeLibLoader.java:54)
        at javafx.graphics@20/com.sun.prism.sw.SWPipeline.lambda$static$0(SWPipeline.java:43)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:319)
        at javafx.graphics@20/com.sun.prism.sw.SWPipeline.<clinit>(SWPipeline.java:42)
        at java.base/java.lang.Class.forName0(Native Method)
        at java.base/java.lang.Class.forName(Class.java:391)
        at java.base/java.lang.Class.forName(Class.java:382)
        at javafx.graphics@20/com.sun.prism.GraphicsPipeline.createPipeline(GraphicsPipeline.java:218)
        at javafx.graphics@20/com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.init(QuantumRenderer.java:92)
        at javafx.graphics@20/com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run(QuantumRenderer.java:125)
        at java.base/java.lang.Thread.run(Thread.java:1623)
Graphics Device initialization failed for :  es2, sw
Error initializing QuantumRenderer: no suitable pipeline found
java.lang.RuntimeException: java.lang.RuntimeException: Error initializing QuantumRenderer: no suitable pipeline found
        at javafx.graphics@20/com.sun.javafx.tk.quantum.QuantumRenderer.getInstance(QuantumRenderer.java:283)
        at javafx.graphics@20/com.sun.javafx.tk.quantum.QuantumToolkit.init(QuantumToolkit.java:253)
        at javafx.graphics@20/com.sun.javafx.tk.Toolkit.getToolkit(Toolkit.java:263)
        at javafx.graphics@20/com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:290)
        at javafx.graphics@20/com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:162)
        at javafx.graphics@20/com.sun.javafx.application.LauncherImpl.startToolkit(LauncherImpl.java:651)
        at javafx.graphics@20/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:409)
        at javafx.graphics@20/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
        at java.base/java.lang.reflect.Method.invoke(Method.java:578)
        at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1081)
Caused by: java.lang.RuntimeException: Error initializing QuantumRenderer: no suitable pipeline found
        at javafx.graphics@20/com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.init(QuantumRenderer.java:95)
        at javafx.graphics@20/com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run(QuantumRenderer.java:125)
        at java.base/java.lang.Thread.run(Thread.java:1623)
Exception in thread "main" java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
        at java.base/java.lang.reflect.Method.invoke(Method.java:578)
        at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1081)
Caused by: java.lang.RuntimeException: No toolkit found
        at javafx.graphics@20/com.sun.javafx.tk.Toolkit.getToolkit(Toolkit.java:275)
        at javafx.graphics@20/com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:290)
        at javafx.graphics@20/com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:162)
        at javafx.graphics@20/com.sun.javafx.application.LauncherImpl.startToolkit(LauncherImpl.java:651)
        at javafx.graphics@20/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:409)
        at javafx.graphics@20/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
        ... 2 more

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':run'.
> Process 'command '/Users/claretian/.sdkman/candidates/java/20.0.2-amzn/bin/java'' finished with non-zero exit value 1

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.5.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 2s
3 actionable tasks: 2 executed, 1 up-to-date

我很确定这与架构不兼容有关,但我已经尝试下载我能找到的每个版本的JavaFX。

u5rb5r59

u5rb5r591#

tl; dr

你似乎有两个问题:

  • 缺少模块化应用程序所需的module-java.java文件
  • JavaFX安装错误,适用于英特尔芯片,而不是像M1这样的苹果芯片。

模块化

显然,您的配置的某些部分或某些库希望您的项目按照Java Platform Module System (JPMS)进行模块化。我这样说是因为你的错误:
项目:=>未找到www.example.com
module-java.java文件是指定模块配置的地方。显然你没有。

获取JavaFX技术

访问JavaFX的三种途径:

  • 手动下载JavaFX实现。参见Gluon downloads page
  • 让Gradle下载库成为您开发项目的一部分
  • 使用与JavaFX/OpenJFX库捆绑的JDK进行开发和部署

对于刚开始涉足JavaFX的人,我建议使用JDK选项。

下载库

您可以告诉Gradle下载实现JavaFX的必要OpenJFX库。
一种简单的方法是使用与IntelliJIDE捆绑在一起的JavaFX新项目模板。

将版本号更新为最新版本后,build.gradle文件可能如下所示。

plugins {
    id 'java'
    id 'application'
    id 'org.javamodularity.moduleplugin' version '1.8.12'
    id 'org.openjfx.javafxplugin' version '0.0.13'
    id 'org.beryx.jlink' version '2.26.0'
}

group 'work.basil.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

ext {
    junitVersion = '5.10.0'
}

sourceCompatibility = '20'
targetCompatibility = '20'

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

application {
    mainModule = 'work.basil.example.exfx'
    mainClass = 'work.basil.example.exfx.HelloApplication'
}

javafx {
    version = '20.0.2'
    modules = ['javafx.controls', 'javafx.fxml']
}

dependencies {

    testImplementation("org.junit.jupiter:junit-jupiter-api:${junitVersion}")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")
}

test {
    useJUnitPlatform()
}

jlink {
    imageZip = project.file("${buildDir}/distributions/app-${javafx.platform.classifier}.zip")
    options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
    launcher {
        name = 'app'
    }
}

jlinkZip {
    group = 'distribution'
}
    • 警告.**IntelliJ的JavaFX新项目模板的Gradle版本的一个主要问题:该项目未针对Java Platform Module System (JPMS)进行模块化。这可能是好的涉足与JavaFX。但是对于专业开发,您可能希望您的项目是模块化的,这样您就可以使用jlink捆绑一个精简的JVM作为最终应用程序工件的一部分。这种模块化的缺乏让我感到惊讶,因为他们的新项目模板的Maven版本确实生成了一个已经配置为模块化的项目。

JDK捆绑JavaFX

几个JDK供应商中至少有两个提供了一个版本的JDK产品,其中包括实现JavaFX的必要OpenJFX库。

这些公司也可能出售对JavaFX的支持,JavaFX的联合领导者Gluon和Oracle也可能这样做。
为了最容易地下载和安装这些JDK,我建议在Mac上安装SDKMAN!。这个方便的shell脚本包使得下载/安装/卸载工作变得非常简单和方便。就运行这个:

% sdk install java 17.0.8.fx-zulu

......得到这个:

Downloading: java 17.0.8.fx-zulu

In progress...

########################################################################################################################### 100.0%

Repackaging Java 17.0.8.fx-zulu...

Done repackaging...

Installing: java 17.0.8.fx-zulu
Done installing!

Do you want java 17.0.8.fx-zulu to be set as default? (Y/n): Y

要查看通过SDKMAN提供的所有JDK产品的列表!,运行以下命令:

sdk list java

原生代码

要知道JavaFX不是纯Java。所需的库包含一些特定于特定主机OS和特定主机芯片(instruction set architecture)的本机代码。
对于您的M1 Mac,您需要在具有Apple Silicon的Mac上运行macOS的JavaFX版本。您可能会看到与Apple Silicon兼容的aarch64ARM选项。

  • 我猜Gradle必须自动检测您的主机环境并下载相应的版本。
  • 如果使用JDK路由,对于您的特定Mac,请确保在Apple Silicon/aarch65/ARM上获得macOS版本,而不是在旧Mac上获得Intel/x64/x86 - 64版本。

您的错误消息表明您确实安装了错误的芯片架构:
mach-o文件,但是不兼容的体系结构(have(x86_64),need(arm64e))

相关问题