包 javafx.util 不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/55170520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
package javafx.util does not exist
提问by LED Fantom
I am running java version 11.0.2 on a Mac. WHen I compiled a java file that imports javafx.util.pair
I got an error: package javafx.util does not exist
. There are not many useful resources to solve this issue. I tried different suggestions online, but in no vain. Currently I am trying to add the JavaFX package to my Java directory. But it doesn't work.
我在 Mac 上运行 Java 版本 11.0.2。当我编译一个导入的 java 文件时,javafx.util.pair
我得到了一个error: package javafx.util does not exist
. 没有多少有用的资源来解决这个问题。我在网上尝试了不同的建议,但没有白费。目前我正在尝试将 JavaFX 包添加到我的 Java 目录中。但它不起作用。
Here is what I did:
这是我所做的:
Downloaded javafx-sdk-11.0.2 folder. Inside the folder, there are 2 directories - legal and lib
Moved items in
legal
dir to/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/legal
Move items in
lib
dir to/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/lib
Reopen Terminal to compile this .java file.
下载 javafx-sdk-11.0.2 文件夹。在文件夹内,有 2 个目录 - legal 和 lib
将
legal
目录中的项目移动到/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/legal
将
lib
目录中的项目移动到/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home/lib
重新打开终端以编译此 .java 文件。
Question_1: Did I miss anything above?
问题_1:我是否遗漏了上述任何内容?
Question_2: If this approach is wrong, what would you suggest?
问题2:如果这种方法是错误的,您有什么建议?
采纳答案by José Pereda
javafx.util.Pair
is part of the javafx.base
module, and as you have mentioned, you need to download the JavaFX SDK to your machine, as it is no longer part of the Java JDK anymore.
javafx.util.Pair
是javafx.base
模块的一部分,正如您所提到的,您需要将 JavaFX SDK 下载到您的机器上,因为它不再是 Java JDK 的一部分。
You can follow the OpenJFX docson how you can get started.
您可以按照OpenJFX 文档了解如何开始。
Once you have downloadedthe SDK, if you want to run JavaFX 11, you will see that you need to do something as documented:
一旦你已经下载了SDK,如果你想运行的JavaFX 11,你会看到,你需要做的事情的记录:
export PATH_TO_FX=path/to/javafx-sdk-11.0.2/lib
javac --module-path $PATH_TO_FX --add-modules=javafx.controls HelloFX.java
java --module-path $PATH_TO_FX --add-modules=javafx.controls HelloFX
This means that you run your Java 11 and include the modules from the JavaFX SDK independent location. Note that javafx.controls
has as transitive dependencies the javafx.base
and the javafx.graphics
modules.
这意味着您运行 Java 11 并包含来自 JavaFX SDK 独立位置的模块。请注意,javafx.controls
具有作为传递依赖项的javafx.base
和javafx.graphics
模块。
However, you won't read in those docs that you have to copy the JavaFX files into the JDK. That won't work.
但是,您不会阅读那些必须将 JavaFX 文件复制到 JDK 的文档。那行不通。
The main reason why it won't work: Java 11 and JavaFX 11 are modular, and even if you see the JavaFX jars under path/to/javafx-sdk-11.0.2/lib
, the JDK uses a big file: /path/to/jdk-11.0.2.jdk/Contents/Home/lib/modules
to run the java
command. That file was created when the JDK was built, therefore adding any jar to it won't have any effect.
行不通的主要原因:Java 11 和 JavaFX 11 是模块化的,即使你在 下看到 JavaFX jars path/to/javafx-sdk-11.0.2/lib
,JDK 也使用一个大文件:/path/to/jdk-11.0.2.jdk/Contents/Home/lib/modules
来运行java
命令。该文件是在构建 JDK 时创建的,因此向其中添加任何 jar 都不会产生任何影响。
Alternative
选择
However, you will find in the docs the rightway to "copy" the JavaFX SDK into the JDK: by creating a new custom image. See the link, section Custom JDK+JavaFX image
.
但是,您会在文档中找到将 JavaFX SDK“复制”到 JDK 中的正确方法:通过创建新的自定义映像。请参阅链接,部分Custom JDK+JavaFX image
。
You can use
jlink
to create a runtime image that includes some or all the JavaFX modules, without being attached to a given project.
您可以使用
jlink
创建包含部分或全部 JavaFX 模块的运行时映像,而无需附加到给定项目。
So you can create a combined image Java11+JavaFX11, and use that as your new JDK. This will allow you getting rid of the --module-path
and --add-modules
arguments (in terms of JavaFX at least).
因此,您可以创建一个组合映像 Java11+JavaFX11,并将其用作新的 JDK。这将允许您摆脱--module-path
和--add-modules
参数(至少在 JavaFX 方面)。
In fact, some distributions like this onealready do this.
事实上,像这样的一些发行版已经这样做了。
So the options are: you use the regular JDK and the JavaFX SDK (with --module-path
and --add-modules
) or you create/use a custom JDK that includes JavaFX.
因此,选项是:您使用常规 JDK 和 JavaFX SDK(带有--module-path
和--add-modules
),或者您创建/使用包含 JavaFX 的自定义 JDK。