Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX: Could not find or load main class only on linux

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
like image 399
DomeWTF Avatar asked May 20 '16 11:05

DomeWTF


2 Answers

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.

like image 74
abulka Avatar answered Oct 05 '22 00:10

abulka


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.

like image 20
Itai Avatar answered Oct 05 '22 00:10

Itai