JavaFX: Could not find or load main class only on linux
Ubuntu, like Debian, has a separate package for OpenJFX (OpenJDK's implementation of JavaFX). Why this is the case, when JavaFX is an integral part of the JRE - I do not know, but your problem should be solved by installing the OpenJFX package:
# aptitude install openjfx
(or using any other package manager), or by using Oracle's JRE.
I ran into this exact problem myself after developing a demo jar called javafx1.jar
using IntelliJ on the Mac and wanting to run it on Linux. After installing the jdk on Ubuntu 19.10 with sudo apt install default-jdk
I also needed to, as @Itai answered, install OpenJFX, which no longer comes bundled with openjdk 11. I used the regular apt command:
sudo apt install openjfx
Critical next step: --> Then as this stackoverflow answer by @Lotfi advises, when running your jar you need to pass the path to those OpenJFX modules. This is what the official docs say, too. So for running javafx1.jar
you say:
java --module-path /usr/share/openjfx/lib --add-modules=javafx.controls,javafx.fxml,javafx.base,javafx.media,javafx.web,javafx.swing -jar javafx1.jar
which is an annoyingly long line to have to use. You can shorten it to specify all modules in that directory using the ALL-MODULE-PATH
parameter:
java --module-path /usr/share/openjfx/lib --add-modules ALL-MODULE-PATH -jar javafx1.jar
P.S. You can find where the javafx module path is on your system by running dpkg-query -L javafx
.
Why java's error message is dumb
The message Could not find or load main class sample.Main
is actually misleading in this case and has nothing to do with not being able to find sample.Main
itself. Dilligently checking the contents of the jar with jar -tf javafx1.jar
and checking that the path sample.Main
is in META-INF/MANIFEST.MF
, as you did - offers no clues.
Because sample.Main
depends on JavaFX and because the latter could not be found, java mischeviously tells you that sample.Main
is the problem, rather than reporting that a dependency is missing - not good behaviour by java IMO. You may be able to use jdeps
to isolate the problem, e.g. jdeps -v javafx1.jar
tells me what is missing.
So
jdeps -v javafx2.jar | grep "not found"
lists that I'm missing stuff, whereas
jdeps --module-path /usr/share/openjfx/lib --add-modules=ALL-MODULE-PATH -v javafx1.jar | grep "not found"
says nothing is missing.
A more efficient solution is to use Oracle JRE on Debians. I observed the same behavior when I built my JavaFX project using InteliJ Idea JavaFX Packager on Windows. Building a project with ordinary Maven did not cause this issue.