How to install & run java 8 and javafx on Ubuntu 18.04

I'd like to install java 8 and javafx on ubuntu 18.04.

I installed openjdk-8-jdk, openjfx and openjdk-8-jre via apt but a compilation of hello world app fails since javac is unable to locate javafx.

$ sudo apt install openjdk-8-jdk openjfx openjdk-8-jre
$ javac -version
javac 1.8.0_191
$ cat App.java
import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.layout.*;

public class App extends Application {
    @Override
    public void start(Stage primaryStage) {
        Pane pane = new Pane();
        Scene scene = new Scene(pane, 300, 250);

        primaryStage.setTitle("Hello, World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
$ javac App.java
App.java:1: error: package javafx.application does not exist
import javafx.application.*;
^
App.java:2: error: package javafx.stage does not exist
import javafx.stage.*;
^
App.java:3: error: package javafx.scene does not exist
import javafx.scene.*;
^
App.java:4: error: package javafx.scene.layout does not exist
import javafx.scene.layout.*;
^
App.java:6: error: cannot find symbol
public class App extends Application {
...

I tried to locate javafx as said here but it not there on my system.

$ ls /usr/lib/jvm/java-8-openjdk-amd64/bin/
appletviewer  java     java-rmi.cgi  jhat   jrunscript  keytool       rmic         servertool  xjc
extcheck      javac    jcmd          jinfo  jsadebugd   native2ascii  rmid         tnameserv
idlj          javadoc  jconsole      jjs    jstack      orbd          rmiregistry  unpack200
jar           javah    jdb           jmap   jstat       pack200       schemagen    wsgen
jarsigner     javap    jdeps         jps    jstatd      policytool    serialver    wsimport

Also it seems like I have 2 versions of java but I installed it only by commands above.

$ ls /usr/lib/jvm/
java-1.8.0-openjdk-amd64  java-8-openjdk-amd64

Solution 1:

The default openjfx package on Ubuntu 18.04 is not compatible with OpenJDK 8. You may use the older version of the openjfx package. Please note that it's not a fully secure solution because you won't get updates for the package.

sudo apt install \
  openjfx=8u161-b12-1ubuntu2 \
  libopenjfx-java=8u161-b12-1ubuntu2 \
  libopenjfx-jni=8u161-b12-1ubuntu2

Hold the package:

sudo apt-mark hold \
  openjfx \
  libopenjfx-java \
  libopenjfx-jni