I've developed a program in Windows with Java(FX) using Intellij Idea and that worked just fine, I then exported the artifact (jar) and there was no problem running it on Windows (both with the console and double clicking it).
I've then copied it to my Ubuntu VM, but there it says
Error: Could not find or load main class sample.Main
This is the Manifest:
Manifest-Version: 1.0
Main-Class: sample.Main
The JAR file structure looks like this:
test.jar
--- META-INF
--- --- MANIFEST.MF
--- org
--- --- json
--- --- --- // json library
--- sample
--- --- Contacts.class
--- --- Controller.class
--- --- Main.class
--- --- sample.fxml
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
.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With