如何在 Java 11 中将 JavaFX 运行时添加到 Eclipse?

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

How to add JavaFX runtime to Eclipse in Java 11?

javajava-11javafx-11

提问by user3520615

I am getting the following error as Java 11 excluded the JavaFX as part of the latest version.

我收到以下错误,因为 Java 11 将 JavaFX 作为最新版本的一部分排除在外。

Error: JavaFX runtime components are missing, and are required to run
this application
Error: JavaFX runtime components are missing, and are required to run
this application

So how can I add JavaFX to Eclipse in Java 11? Thanks.

那么如何在 Java 11 中将 JavaFX 添加到 Eclipse 中呢?谢谢。

采纳答案by José Pereda

Following the getting started guide, these are the required steps to run JavaFX 11 from Eclipse.

按照入门指南,这些是从 Eclipse 运行 JavaFX 11 所需的步骤。

1 Install Eclipse 2018-09 from here.

1 从这里安装 Eclipse 2018-09 。

2 Install JDK 11 from here.

2 从这里安装 JDK 11 。

3 Add Java 11 as an installed JRE to Eclipse: Eclipse -> Window -> Preferences -> Java -> Installed JREs -> Add.

3 将 Java 11 作为已安装的 JRE 添加到 Eclipse:Eclipse -> 窗口 -> 首选项 -> Java -> 已安装的 JRE -> 添加。

4 Download JavaFX 11 ea from here.

4 从这里下载 JavaFX 11 ea 。

5 Create a User Library: Eclipse -> Window -> Preferences -> Java -> Build Path -> User Libraries -> New. Name it JavaFX11 and include the jars under the lib folder from JavaFX 11-ea.

5 创建用户库:Eclipse -> Window -> Preferences -> Java -> Build Path -> User Libraries -> New。将其命名为 JavaFX11,并在 JavaFX 11-ea 的 lib 文件夹下包含 jar。

6 Create a Java project. You don't need to add a module-path class. Make sure that you select Java 11 and you add the JavaFX11 library to the project's modulepath.

6 创建 Java 项目。您不需要添加模块路径类。确保选择 Java 11 并将 JavaFX11 库添加到项目的模块路径。

7 Add a package javafx11and the main application class HelloFX:

7 添加一个包javafx11和主应用程序类HelloFX

package javafx11;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;


public class HelloFX extends Application {

    @Override
    public void start(Stage stage) {
        String version = System.getProperty("java.version");
        Label l = new Label ("Hello, JavaFX 11, running on "+version);
        Scene scene = new Scene (new StackPane(l), 300, 200);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }

}

Note that the editor shouldn't complain about JavaFX classes, as we have included the user library.

请注意,编辑器不应抱怨 JavaFX 类,因为我们已经包含了用户库。

8 Add runtime arguments. Edit the project's run configuration, and add these VM arguments:

8 添加运行时参数。编辑项目的运行配置,并添加这些 VM 参数:

--module-path C:\Users\<user>\Downloads\javafx-sdk-11\lib --add-modules=javafx.controls

9 Finally, run the project. It should work fine.

9 最后,运行项目。它应该可以正常工作。

回答by Aleksander Radovan

@José Pereda is perfectly right, everything works as it should. I have tested it with the Eclipse release 2018-12. So, after you download the IDE (https://www.eclipse.org/downloads/packages/release/2018-12/r/eclipse-ide-java-developers) and install the e(fx)clipse plugin, you should be able to create a "JavaFx Project" item in your workspace.

@José Pereda 是完全正确的,一切正常。我已经使用 Eclipse 版本 2018-12 对其进行了测试。因此,在下载 IDE ( https://www.eclipse.org/downloads/packages/release/2018-12/r/eclipse-ide-java-developers) 并安装 e(fx)clipse 插件后,您应该能够在您的工作区中创建一个“JavaFx 项目”项目。

The next step is to delete the "JavaFX SDK" from the "Classpath" module (Properties -> Java Build Path -> Libraries) and add the Maven dependency in order to remove compile errors:

下一步是从“类路径”模块(属性 -> Java 构建路径 -> 库)中删除“JavaFX SDK”并添加 Maven 依赖项以消除编译错误:

<dependency>
   <groupId>org.openjfx</groupId>
   <artifactId>javafx-controls</artifactId>
   <version>11.0.1</version>
</dependency>

JavaFX SDK for deletion

用于删除的 JavaFX SDK

The last step is to add a VM argument to "Run Configuration" of your JavaFX class for starting the application (Run Configurations -> Arguments tab -> VM arguments):

最后一步是将 VM 参数添加到 JavaFX 类的“运行配置”以启动应用程序(运行配置 -> 参数选项卡 -> VM 参数):

--module-path C:\path to the OpenJFX\javafx-sdk-11.0.1\lib --add-modules=javafx.controls

and it should work after you run it. Of course, you should download the JavaFX SDK before that (https://gluonhq.com/products/javafx/), unzip it and use the path to "lib" folder in the aforementioned VM argument.

运行它后它应该可以工作。当然,您应该在此之前下载 JavaFX SDK ( https://gluonhq.com/products/javafx/),解压缩它并使用上述 VM 参数中“lib”文件夹的路径。

回答by Cristian

I'd like to share my findings regarding launching a javafx 11 app. I'm talking about an existing legacy javafx app originally developed without modules (e.g. w/ Java 8).

我想分享我关于启动 javafx 11 应用程序的发现。我说的是一个现有的遗留 javafx 应用程序,最初是在没有模块的情况下开发的(例如,使用 Java 8)。

The most interesting way IMO is Method #2bbelow, which requires the minimum of changes, i.e. it doesn't need -module-pathargument. I mention that I'm using maven, so cf. previous answers the first step is to add a dependency in your pom.xmltowards javafx. E.g.:

IMO 最有趣的方式是下面的方法#2b,它需要最少的更改,即它不需要-module-path参数。我提到我正在使用 maven,所以请参阅。以前的答案 第一步是在您的pom.xmljavafx 中添加依赖项。例如:

<dependency>
  <groupId>org.openjfx</groupId>
  <artifactId>javafx-controls</artifactId>
  <version>11.0.2</version>
</dependency>

In order to launch, I found several options:

为了启动,我找到了几个选项:

1a) Launch using maven from command line

1a) 从命令行使用 maven 启动

Configure in your pom.xmla section like:

在您的pom.xmla 部分中进行配置,例如:

    <build>
        <plugins>
            ...
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>org.openjfx.hellofx.MainApp</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

After this, from a console window you can run mvn exec:java, and this should launch the application. Maven will take care of adding all the entries from classpathto module path. I remind the CTRL + ALT + Tshortcut in Eclipse, that opens a terminal window directly in the IDE.

之后,您可以从控制台窗口运行mvn exec:java,这应该会启动应用程序。Maven 将负责将所有条目从classpath添加到module path。我提醒了CTRL + ALT + TEclipse 中的快捷方式,即直接在 IDE 中打开终端窗口。

1b) Launch using maven, but as an Eclipse launch config

1b) 使用 maven 启动,但作为 Eclipse 启动配置

Right click on the project > Run As > Maven Build.... Then enter exec:javain the Goalstext box. Advantageover the previous method: a bit more integrated to Eclipse. And easy debugging. You only need to relaunch the launch config in Debug mode and that's it. May I remind that the Eclipse launch configs can be stored as .launchfiles directly in the project dir (repo), thus shareable/reusable by colleagues. Use the last tab, Common, in the Edit Configurationwindow.

右键单击项目 > 运行方式 > Maven Build...。然后exec:java目标文本框中输入。与前一种方法相比的优势:与 Eclipse 的集成度更高。并且调试方便。您只需要在调试模式下重新启动启动配置,就是这样。我可以提醒一下,Eclipse 启动配置可以作为.launch文件直接存储在项目目录 (repo) 中,从而可供同事共享/重用。使用最后一个标签,常见,在编辑配置窗口。

2a) Launch using Eclipse and specifying -module-pathusing a variable

2a) 使用 Eclipse 启动并-module-path使用变量指定

Right click on the main class > Run As > Java Application. The first time you click a new launch configuration is created; probably it won't work, so you need to edit it (e.g. **Run button (w/ Play icon) in the toolbar > Run configurations > select the one corresponding to your class. Or CTRL + clickon it after clicking on Play).

右键单击主类 > 运行方式 > Java 应用程序。第一次单击时会创建一个新的启动配置;可能它不起作用,因此您需要对其进行编辑(例如,工具栏中的 **Run 按钮(带播放图标)> 运行配置 > 选择与您的类相对应的那个。或者CTRL + click在单击Play后对其进行编辑)。

Then add this in Arguments > VM arguments:

然后在Arguments > VM arguments 中添加

--module-path ${project_classpath:REPLACE_ME_WITH_YOUR_PROJECT_NAME} --add-modules javafx.controls,javafx.fxml

So the trick here was to use the ${project_classpath}variable. Because otherwise you should have needed to write exactly the path towards the javafx jar, that are somewhere in your .m2repo. Doing this would make the launch config not easily reusable by colleagues.

所以这里的技巧是使用${project_classpath}变量。因为否则你应该需要准确地写出 javafx jar 的路径,它在你的.m2仓库中的某个地方。这样做会使启动配置不容易被同事重用。

2b) Launch using Eclipse WITHOUT specifying -module-path

2b) 使用 Eclipse 启动而不指定 -module-path

This is la piece de résistence, which I found by mistake, after about 5h of Java 11 & modules "fun". Your application can work out of the box, without touching -module-path. The javafx libs will of course still need to be in your classpath (but this is handled by mvn). The trickis (cf. this) that your main app SHOULD NOT extend Application(e.g. MyApplicationbelow). If this is your case, then make a new class with a main()function that does e.g.:

这是la piece de résistence,我在 Java 11 和模块“有趣”大约 5 小时后错误地发现了它。您的应用程序可以开箱即用,无需接触-module-path。javafx 库当然仍需要在您的类路径中(但这由 mvn 处理)。该诀窍是(参见),您的主要应用不应延伸Application(例如MyApplication下文)。如果这是你的情况,那么用一个main()函数创建一个新类,例如:

Application.launch(MyApplication.class);

NOTE

笔记

In my case (i.e. on Windows, having a default JDK 8 in the system), there is a known issue regarding launching javafx. The solution is to add the following VM arg: -Djava.library.path=C:. So actually for all 4 methods described above I needed to add this.

在我的情况下(即在 Windows 上,系统中有一个默认的 JDK 8),有一个关于启动 javafx 的已知问题。解决方案是添加以下 VM arg:-Djava.library.path=C:. 所以实际上对于上面描述的所有 4 种方法,我需要添加它。

回答by carlao2005

I had problems regarding FXMLLoader... It was not able to find the class.

我遇到了关于FXMLLoader... 无法找到课程的问题。

I resolved this by changing the arguments showed above, to load allmodules, not only javafx.controls:

我通过更改上面显示的参数解决了这个问题,以加载所有模块,而不仅仅是javafx.controls

--module-path <path to the javafx sdk> --add-modules=ALL-MODULE-PATH

回答by Andrew S

Using Carlao2005's advice I was able to put together a complete guide for getting JavaFX to work in Eclipse now that JavaFX does not come with the Java SDK. This assumes you have Eclipse with the E(FX)clipse plug in set up and an installation of the JavaFX SDK. Note, it is important that the JavaFX SDK is installed in a location without spaces in the path so C:/Program Files is not a good choice.

使用 Carlao2005 的建议,我能够将 JavaFX 放在 Eclipse 中的完整指南放在一起,因为 JavaFX 没有随 Java SDK 一起提供。这假设您有安装了 E(FX)clipse 插件并安装了 JavaFX SDK 的 Eclipse。请注意,JavaFX SDK 安装在路径中没有空格的位置很重要,因此 C:/Program Files 不是一个好的选择。

Go through this step for each new Java FX project to be built. First, go to file > new > otherand find the option for creating a new Java FX Project. Once the project is created you will notice it does not compile as Eclipse needs to know about the JavaFX libraries. Access the project propertiesand then find the Java build path. Select the Libraries taband then module path. Then select the add librarybutton and then select the User libraryoption before pressing next.

为每个要构建的新 Java FX 项目执行此步骤。首先,转到file > new > other并找到用于创建新Java FX Project的选项。创建项目后,您会注意到它不会编译,因为 Eclipse 需要了解 JavaFX 库。访问项目属性,然后找到Java 构建路径。选择库选项卡,然后 选择模块路径。然后选择添加库按钮,然后在按下一步之前选择用户库选项 。

On the window that appears, select newand name the library JavaFX13. Select add external jarsand then find the libfolder of your JavaFX 13installation directory and select all of the .jar folders then apply and close or finish on all windows.

在出现的窗口中,选择 new并将库命名为JavaFX13。选择添加外部 jar,然后找到JavaFX 13安装目录的lib文件夹并选择所有 .jar 文件夹,然后在所有窗口上应用并关闭或完成。

If you attempt to run the project you will find it will not be able to find application.main. To fix this we have one last step. Right click main.java in Eclipse's package explorer. Select run as >run configurationsand then select the arguments tab. In the vm arguments add the following (where the module path points to your installation of the Java FX sdk):

如果您尝试运行该项目,您会发现它无法找到application.main。为了解决这个问题,我们还有最后一步。在 Eclipse 的包浏览器中右键单击 main.java。选择运行方式 >运行配置,然后选择参数选项卡。在 vm 参数中添加以下内容(模块路径指向您安装的 Java FX sdk):

--module-path C:\Programs\Java\javafx-sdk-13.0.1\lib --add-modules=ALL-MODULE-PATH

That's it, you should now be able to run the Java FX project and see a default window. If running on Mac OS de-select the option -XstartOnFirstThread argument when launching SWT if you experience problems getting a window to display.

就是这样,您现在应该能够运行 Java FX 项目并看到一个默认窗口。如果在 Mac OS 上运行,如果您在显示窗口时遇到问题,请在启动 SWT 时取消选择选项 -XstartOnFirstThread 参数。

回答by Chris

As mentioned by @Cristian, creating a separate Main class, seems to do the trick, without any other arguments.

正如@Cristian 所提到的,创建一个单独的 Main 类似乎可以解决问题,没有任何其他参数。

public class Main {
    public static void main(String[] args) {
        Application.launch(MainWindow.class);
    }
}

public class MainWindow extends Application {
    @Override
    public void start(Stage stage) {
        String javaVersion = System.getProperty("java.version");
        String javafxVersion = System.getProperty("javafx.version");
        Label l = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
        Scene scene = new Scene(new StackPane(l), 640, 480);
        stage.setScene(scene);
        stage.show();
    }

    // do not use this method
    //  public static void main(String[] args) {
    //      Application.launch(MainWindow.class);
    //  }
}