导入javafx无法解析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32630354/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:52:54  来源:igfitidea点击:

The import javafx cannot be resolved

javaeclipsejavafx

提问by Farouk Sabry

I installed Eclipse IDE today on my Ubuntu Linux and then installed JavaFX using 'Install New Software' and when I created a javafx project, I got the following error in Main.java:

我今天在我的 Ubuntu Linux 上安装了 Eclipse IDE,然后使用“安装新软件”安装了 JavaFX,当我创建一个 javafx 项目时,我在 Main.java 中收到以下错误:

The import javafx cannot be resolved.

无法解析导入 javafx。

So, I listed the following directory to search for "jfxrt.jar":

所以,我列出了以下目录来搜索“jfxrt.jar”:

ls -l /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext

But I didn't find "jfxrt.jar".

但我没有找到“jfxrt.jar”。

java -version

The output:

输出:

openjdk version "1.8.0_45-internal"

openjdk 版本“1.8.0_45-内部”

OpenJDK Runtime Environment (build 1.8.0_45-internal-b14)

OpenJDK 运行时环境(构建 1.8.0_45-internal-b14)

OpenJDK 64-Bit Server VM (build 25.45-b02, mixed mode)

OpenJDK 64 位服务器 VM(构建 25.45-b02,混合模式)

回答by MWiesner

According to the packages listin Ubuntu Vividthere is a package named openjfx. This should be a candidate for what you're looking for:

根据Ubuntu Vivid 中包列表,有一个名为openjfx的包。这应该是您正在寻找的候选人:

JavaFX/OpenJFX 8 - Rich client application platform for Java

JavaFX/OpenJFX 8 - Java 的富客户端应用程序平台

You can install it via:

您可以通过以下方式安装它:

sudo apt-get install openjfx

It provides the following JAR filesto the OpenJDK installation on Ubuntu systems:

它为Ubuntu 系统上的 OpenJDK 安装提供以下 JAR 文件

/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/jfxrt.jar
/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/jfxswt.jar
/usr/lib/jvm/java-8-openjdk-amd64/lib/ant-javafx.jar
/usr/lib/jvm/java-8-openjdk-amd64/lib/javafx-mx.jar

Hope this helps.

希望这可以帮助。

回答by Mark Burleigh

For Java Compiler 8 or above, do the following:

对于 Java Compiler 8 或更高版本,请执行以下操作:

  1. Right-click on project
  2. Choose Build Path ----> Add Libraries
  1. 右键单击项目
  2. 选择构建路径---->添加库

You will then be presented with the following screenshot below:

然后,您将看到以下屏幕截图:

Choose a JDK 8 or above

选择 JDK 8 或更高版本

Make sure you have downloaded and installed a JDK 8 or above

确保您已经下载并安装了 JDK 8 或更高版本

When you press the finish button, all Java FX errors in your code should disappear.

当您按下完成按钮时,代码中的所有 Java FX 错误都应该消失。

Note Prerequisites:

注意先决条件:

JDK 9 installed and tested on NetBeans 8.0.1

在 NetBeans 8.0.1 上安装并测试了 JDK 9

回答by Jose Manuel Gomez Alvarez

A) Make sure that you are using a compatible JDK, like 1.8, AND

A) 确保您使用的是兼容的 JDK,例如 1.8,并且

B) configure a compatible version of Java in the Eclipse Project Facets.

B) 在 Eclipse Project Facets 中配置兼容的 Java 版本。

  1. Right click on the Java project, select Properties
  2. Select Project Facets, find Java, set version 1.8
  1. 右键单击 Java 项目,选择 Properties
  2. 选择Project Facets,找到Java,设置1.8版

For Java 11, this error also appears, as JavaFX has been removed from Java 11 and comes as standalone

对于 Java 11,也会出现此错误,因为 JavaFX 已从 Java 11 中删除并作为独立版本出现

More info: https://blogs.oracle.com/java-platform-group/the-future-of-javafx-and-other-java-client-roadmap-updates

更多信息:https: //blogs.oracle.com/java-platform-group/the-future-of-javafx-and-other-java-client-roadmap-updates

回答by Ondra ?i?ka

Here's how to set it up on Ubuntu Linuxwith Maven:

以下是使用Maven在 Ubuntu Linux上设置它的方法:

1) Install OpenJFX package, check where did it put the files.

1) 安装 OpenJFX 包,检查它把文件放在哪里。

      sudo apt install openjfx
      dpkg-query -L openjfx

You might end up with version for JDK 11. In which case, either follow with installing a new OpenJDK, or set a version of OpenJFX for JDK 8.

你最终可能会得到 JDK 11 的版本。在这种情况下,要么安装一个新的 OpenJDK,要么为 JDK 8 设置一个 OpenJFX 版本。

2) Put it to your Maven project as a system-scoped dependency.

2) 将其作为system-scoped 依赖项放入您的 Maven 项目。

Note this is the lazy and not-so-nice way. Properly, you should install the jars like this:

dpkg-query -L openjfx | grep -E '.jar$' | xargs -l -I{} \
  mvn install:install-file -Dfile="{}" -DgroupId=javafx \
    -DartifactId=$(echo $JAR | tr '.' '-') -Dversion=1.0 -Dpackaging=jar

And then use it as a normal compile-scopeddependency.

请注意,这是一种懒惰且不太好的方式。正确地,您应该像这样安装罐子:

dpkg-query -L openjfx | grep -E '.jar$' | xargs -l -I{} \
  mvn install:install-file -Dfile="{}" -DgroupId=javafx \
    -DartifactId=$(echo $JAR | tr '.' '-') -Dversion=1.0 -Dpackaging=jar

然后将其用作普通compile-scoped依赖项。

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.source.level>1.8</project.source.level>
    <project.target.level>1.8</project.target.level>

    <javafx.dir>/usr/share/openjfx/lib</javafx.dir>
</properties>

<dependencies>

    <!-- JavaFx :
      sudo apt install openjfx
      dpkg-query -L openjfx
    -->
    <dependency>
        <groupId>javafx</groupId>
        <artifactId>javafx-base</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${javafx.dir}/javafx.base.jar</systemPath>
    </dependency>
    <dependency>
        <groupId>javafx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${javafx.dir}/javafx.controls.jar</systemPath>
    </dependency>
    <dependency>
        <groupId>javafx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${javafx.dir}/javafx.fxml.jar</systemPath>
    </dependency>
    <dependency>
        <groupId>javafx</groupId>
        <artifactId>javafx-graphics</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${javafx.dir}/javafx.graphics.jar</systemPath>
    </dependency>
    <dependency>
        <groupId>javafx</groupId>
        <artifactId>javafx-media</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${javafx.dir}/javafx.media.jar</systemPath>
    </dependency>
    <dependency>
        <groupId>javafx</groupId>
        <artifactId>javafx-swing</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${javafx.dir}/javafx.swing.jar</systemPath>
    </dependency>
    <dependency>
        <groupId>javafx</groupId>
        <artifactId>javafx-web</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${javafx.dir}/javafx.web.jar</systemPath>
    </dependency>

</dependencies>