how to add javafx dependencies in maven with java

2019-05-24 23:17发布

I switched to ubuntu 18.04. Which has java 10 as default jvm

Now my apps that use javafx cannot compile anymore.

 cannot find symbol
[ERROR]   symbol:   class ObservableMap

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>

result :

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] module not found: javafx.graphics

of course, java --list-modules | grep fx returns nothing.

I've spent more than 10 hours trying to figure this out.

TL:DR What am I supposed to do to compile my JavaFX modules with Java 10?

minimum project :

/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

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 {}

}

error :

java10fx/src/main/java/MyApp.java:[1,26] package javafx.application does not exist

2条回答
劳资没心,怎么记你
2楼-- · 2019-05-24 23:20

ok so actually the issue is kinda complex.

The OPENJDK version installed with ubuntu is a mix between java 10 and java 11 : the package installed is actually "openjdk-11" but the VM installed refers to itself as openjdk-10 . Just as openjdk-11 it does not provide the javafx modules.

The solution was to install the oracle jdk 10:

sudo add-apt-repository ppa:linuxuprising/java
sudo apt install oracle-java10-installer

And then the minimum program I gave works.

Also I purged everything eg sudo apt remove --purge "openjdk-11*"

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-05-24 23:41

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).

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.

查看更多
登录 后发表回答