如何使用Java 10在Maven中添加Javafx依赖项 [英] how to add javafx dependencies in maven with java 10
问题描述
我切换到ubuntu 18.04.哪个具有Java 10作为默认jvm
I switched to ubuntu 18.04. Which has java 10 as default jvm
现在,我使用javafx的应用无法编译了.
Now my apps that use javafx cannot compile anymore.
cannot find symbol
[ERROR] symbol: class ObservableMap
我试图向maven-compiler-plugin添加参数以加载javafx.graphics模块.
I tried to add parameters to the maven-compiler-plugin to load the javafx.graphics module.
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>
<arg>--add-modules</arg>
<arg>javafx.graphics</arg>
</compilerArgs>
</configuration>
</plugin>
结果:
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] module not found: javafx.graphics
当然是java --list-modules |grep fx不返回任何内容.
of course, java --list-modules | grep fx returns nothing.
我花了十多个小时来解决这个问题.
I've spent more than 10 hours trying to figure this out.
TL:DR我应该怎么做才能用Java 10编译JavaFX模块?
TL:DR What am I supposed to do to compile my JavaFX modules with Java 10?
最小项目:
/pom.xml
<project>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<name>java10fx</name>
<artifactId>java10fx</artifactId>
<version>0.0.1</version>
<groupId>my.test</groupId>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<release>10</release>
</configuration>
</plugin>
</plugins>
</build>
</project>
/src/main/java/MyApp.java
/src/main/java/MyApp.java
import javafx.application.Application;
import javafx.stage.Stage;
public class MyApp extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {}
}
错误:
java10fx/src/main/java/MyApp.java:[1,26] package javafx.application does not exist
推荐答案
OpenJDK从未包含JavaFX,并且从Java 11开始,Oracle JDK都不包含JavaFX,因此JavaFX必须成为常规依赖项.OpenJFX是JavaFX背后的项目,最近早期发布访问可用于Java 10和11的独立JavaFX SDK的构建,但尚未在Maven Central上使用(已计划在将来使用).
OpenJDK never contained JavaFX and from Java 11 on, neither will Oracle JDK, so JavaFX will have to become a regular dependency. OpenJFX, the project behind JavaFX, has recently released an early access build of a standalone JavaFX SDK that works with Java 10 and 11, but it is not yet available on Maven Central (that is planned for the future).
现在,您必须手动下载SDK ,并找到一种将其添加到的方法您的Maven构建,例如将其部署到Nexus或将其包含在您的仓库中的文件夹中.一旦它在Maven Central上可用,您就可以像使用任何其他依赖项一样使用它.
For now, you have to download the SDK manually and find a way to add it to your Maven build, for example by deploying it to your Nexus or including it in a folder in your repo. Once it is available on Maven Central you will be able to use it just like any other dependency.
这篇关于如何使用Java 10在Maven中添加Javafx依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!