Maven dependency for JavaFX using OpenJDK 1.8

KasparJohannes picture KasparJohannes · May 28, 2019 · Viewed 12.2k times · Source

Environment:

  • OS: For developing and running the application, both Windows 10 Pro (1809) and Linux (something like Ubuntu or Debian) with OpenJDK 1.8 and Maven installed will be used.
  • JDK: OpenJDK 1.8
  • IDE: IntelliJ Ultimate
  • Build tool Maven (multi-module project, one module uses JavaFX)
  • I'd very much prefer not to use anything with a paid license (even though, for this project, I could probably use the Oracle JDK for free...).

The problem:

I know, that the JDK 1.8 is supposed to come with JavaFX included. However, for OpenJDK 1.8 this, somehow, seems not to be the case (without the Maven dependencies, IntelliJ doesn't accept import javafx.application.Application; nor any other JFX imports).

For this reason, I am trying to include it as a Maven dependency. If I use:

    <!-- JavaFX -->
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>11</version>
    </dependency>
    .
    .
    .

I get the following error message:

Error:(3, 26) java: cannot access javafx.application.Application
   bad class file: /C:/Users/kaspa/.m2/repository/org/openjfx/javafx- 
   graphics/11/javafx-graphics-11-win.jar!/javafx/application/Application.class
   class file has wrong version 54.0, should be 52.0
   Please remove or make sure it appears in the correct subdirectory of the 
   classpath.

Using <version>12</version> yields [...]wrong version 55.0, should be 52.0[...].

The lowest <version>, inside the Maven <dependency>, I am able to use seems to bo 11.

The Question:

  • Is there something I can add/change to my pom.xml to make my application compile and run?
  • Do I absolutely need to upgrade to OpenJDK 11 or 12?

Answer

Gopala Krishna Char picture Gopala Krishna Char · May 28, 2019

First, a bit of background. From Java 8 to Java 10, the Oracle JDK used to come with JavaFX. But, even in these versions, JavaFX is not part of the OpenJDK. From Java 11, JavaFx is also not part of the Oracle JDK (as was the case with OpenJDK).

So, What are you doing wrong?

You are trying to run JavaFX 11 and 12 with JDK 8. This is not possible.

What can you do?

Get the openjfx 8. You can either build them from source or install it through sudo-apt on Ubuntu.

Is JavaFX 8 available as Maven dependency?

Not that I am aware of!

My advice.. Use Oracle JDK if you want to use Java8 or upgrade to Java 11 and use openJFX as Maven dependency.