jgneff/openjfx

java.lang.NoClassDefFoundError: javafx/application/Application on Ubuntu 20.04.4

jrd10 opened this issue · 8 comments

jrd10 commented

Hi John and all the team, thank you for Openjfx

I use a Java application and I have this error message:
"Impossible to find or to load the main class..."...

Erreur : impossible de trouver ou de charger la classe principale rg.espfx.EspFx
Causé par : java.lang.NoClassDefFoundError: javafx/application/Application

Below the complete error message.

Please, note that the editor recommand to use the Oracle environment.

Thank you for your feedback and confirm if, in this case, it is better to use the Oracle environment.
Please note, I am not an expert with Java :).

The application: https://thopt.s4e2.com/jml/en/thermoptim-en/download

My environment is:

I am on Ubuntu 20.04.4, and I have installed openjfx with Snap.

 jrd_10@my-pc:~/...$ snap info openjfx
name:      openjfx
summary:   Current JavaFX release and early-access builds
publisher: John Neffenger (jgneff)
store-url: https://snapcraft.io/openjfx
contact:   https://github.com/jgneff/openjfx/discussions
license:   GPL-2.0-with-classpath-exception
description: |
  OpenJFX is the official open-source project that develops JavaFX,
  a cross-platform application framework for developing and deploying
  rich client applications that operate consistently across a diverse
  set of platforms.
  
  This package, together with OpenJDK 11 or later, provides everything
  you need to develop a JavaFX application on Linux, including all
  of the latest JAR files, native libraries, JMOD archives, API
  documentation, and source code of JavaFX.
  
  To get started, see the README file for this package on GitHub:
  
  https://github.com/jgneff/openjfx
  
  Java and OpenJDK are trademarks or registered trademarks of Oracle
  and/or its affiliates.
commands:
  - openjfx
snap-id:      FpIMJxPmRiPFwL2hGqujo314Hkiwu5XY
tracking:     latest/edge
refresh-date: yesterday at 19:29 CEST
channels:
  latest/stable:    19+11 2022-09-25 (536) 103MB -
  latest/candidate: ↑                            
  latest/beta:      ↑                            
  latest/edge:      20+3  2022-09-25 (535) 103MB -
installed:          20+3             (535) 103MB -

Java version:

jrd_10@my-pc:~/...$ java -version
openjdk version "11.0.16" 2022-07-19
OpenJDK Runtime Environment (build 11.0.16+8-post-Ubuntu-0ubuntu120.04)
OpenJDK 64-Bit Server VM (build 11.0.16+8-post-Ubuntu-0ubuntu120.04, mixed mode, sharing)

The complete error message is:

jrd_10@my-pc:~/...$ ./NavThopt
commande Java:  /usr/bin/java
Installed java version: 2022-07-19
Required JRE version: 1.4+..OK
AppRoot:  .
/home/jrd_10/Desktop/NavThopt_linux_282_Fr/NavThopt_linux
+ exec /usr/bin/java -cp lib/bibTEP.jar:lib/gef2.jar:lib/im2.jar:lib/inth2.jar:lib/imStruc2.jar:lib/ThoptDemo2.jar:lib/ThoptEdu2.jar:lib/Thopt2.jar:lib/EspFx.jar:extBib.zip:extUser2.zip:extThopt2.zip rg.espfx.EspFx
Erreur : impossible de trouver ou de charger la classe principale rg.espfx.EspFx
Causé par : java.lang.NoClassDefFoundError: javafx/application/Application

Thank you for your time.

Your java command is missing the module path argument (--module-path or -p). You need to put the directory containing the JavaFX modular JAR files on the module path for the Java launcher to find them. Note that it's not enough to put the JavaFX SDK library directory on the class path

$ java -cp "/snap/openjfx/current/sdk/lib/*" -jar dist/hello-javafx-1.0.0.jar 
Error: Could not find or load main class org.status6.hello.javafx.Hello
Caused by: java.lang.NoClassDefFoundError: javafx/application/Application

Invoking the Java application from its main class fails, too:

$ java -cp "dist/hello-javafx-1.0.0.jar:/snap/openjfx/current/sdk/lib/*" org.status6.hello.javafx.Hello
Error: JavaFX runtime components are missing, and are required to run this application

See the notes in the documentation for the following:

which state:

Note: The JavaFX classes must be loaded from a set of named javafx.* modules on the module path. Loading the JavaFX classes from the classpath is not supported. A warning is logged when the JavaFX runtime is started if the JavaFX classes are not loaded from the expected named module. This warning is logged regardless of whether the JavaFX runtime was started by calling this method or automatically as described above.

Also see the Important Changes section at the top of the Release Notes for JavaFX 16.

Start your application with the JavaFX SDK library on the module path as in the example below, and it should work. This example starts the application as the main class in a JAR file:

$ java -p /snap/openjfx/current/sdk/lib --add-modules javafx.controls -jar dist/hello-javafx-1.0.0.jar 
Hello World!

You don't need to make your application modular, but if you do, you can also start it as the main class in a module:

$ java -p dist/hello-javafx-1.0.0.jar:/snap/openjfx/current/sdk/lib -m org.status6.hello.javafx
Hello World!
jrd10 commented

Hello, thank you for your fast feedback.
I am not an expert in Java.

What I can say is, if I run openjfx, terminal returns /var/snap/openjfx/535/openjfx.env

As file to run the app there is no .jar file, only .sh file or without extension.

In the directory where the app is, I tried something like
java -p /snap/openjfx/current/sdk/lib --add-modules javafx.controls ./NavThoptLinuxD

I've got this error message...

Error occurred during initialization of boot layer
java.lang.module.FindException: Error reading module: /snap/openjfx/current/sdk/lib/javafx.base.jar
Caused by: java.lang.module.InvalidModuleDescriptorException: Unsupported major.minor version 61.0

The editor recommend to use Oracle environment, not the openJDK.
Due to my lack of knowledge in Java, I am going to change my environment.

Thank you for your time and interest.

You can fix this latest error:

Caused by: java.lang.module.InvalidModuleDescriptorException: Unsupported major.minor version 61.0

by switching to OpenJFX 19 on the "stable" channel instead of version 20 on the "edge" channel:

$ sudo snap refresh openjfx --stable 
openjfx 19+11 from John Neffenger (jgneff) refreshed

JavaFX 19 requires JDK 11 or later to run, but JavaFX 20 needs JDK 17 or later. The message, "Unsupported major.minor version 61.0," means you're trying to run the JavaFX Java class files built for JDK 17 (class major version 61) with some older version of Java which does not recognize that class format.

As file to run the app there is no .jar file, only .sh file or without extension.

The OpenJFX Snap package is a Software Development Kit (SDK), not an app. See the Usage section in the README file to understand how to use it.

The editor recommend to use Oracle environment, not the openJDK.

You can ignore that recommendation. They are essentially the same thing now. Starting with JDK 11, the Oracle JDK and OpenJDK "builds are functionally identical aside from some cosmetic and packaging differences." See this Oracle blog post for details. Oracle even provides its own build of the latest OpenJDK 19 that you can download and install. Or you can install my OpenJDK Snap package built to work with the OpenJFX Snap package:

$ sudo snap install openjdk

Due to my lack of knowledge in Java, I am going to change my environment.

You just need to do one or both of the following:

  1. either upgrade your Java to the latest OpenJDK 19, available from Oracle and many other places, or
  2. downgrade your copy of OpenJFX to version 19 (not 20) so that it will work with JDK 11 or later.

I recommend doing both: get on the "stable" channel for OpenJFX 19 and upgrade your Java to the latest OpenJDK 19. The latest version 19 of both are the only versions currently supported for free.

Sorry, I just realized you are not a JavaFX software developer. I assumed you were the author of the Thermoptim browser application, but you clearly state in your first comment that you're just trying to use it. I'm sorry for the misunderstanding.

In this case, you should do exactly what's recommended on the Thermoptim download page for "Installing the Oracle JRE on Ubuntu." The link to the Oracle Java 8 runtime that they recommend has built-in support for JavaFX. Just follow their installation instructions, and it should work fine. The current Java 8 download file for 64-bit Linux is jre-8u341-linux-x64.tar.gz.

The OpenJFX Snap package is a Software Development Kit intended to be used only by Java software developers writing JavaFX applications.

jrd10 commented

Hi @jgneff thank you for your time and sorry if I have not been clear.
Yes, I will do that.

At the same time, I have a question for a Java expert :).

Could we install Java on Ubuntu Server environment and make Java application running?
Or do we need necessarily a desktop environment

Thank you for your feedback.

Take care. José

Could we install Java on Ubuntu Server environment and make Java application running?

Yes, if you have a fast network connection. You could use X11 forwarding to run applications on the server with their windows displayed on your local system. For example, I installed Ubuntu 22.04.1 LTS Server in an LXD container called jammy (with no desktop environment at all) and added the default OpenJDK 11 runtime as follows:

ubuntu@jammy:~$ sudo apt-get install default-jre

When I connect using SSH with X11 forwarding and run a remote "Hello World" Java Swing application, its window appears on my local system:

john@tower:~$ ssh -X jammy.lxd
Welcome to Ubuntu 22.04.1 LTS (GNU/Linux 5.15.0-48-generic x86_64)
  ...
ubuntu@jammy:~$ java -jar lib/hello-swing-1.0.0.jar 
Hello World!

Or do we need necessarily a desktop environment

You could also install a full desktop environment on the server and run applications there using a remote desktop protocol such as VNC. If you want to try this yourself, there's a way on Ubuntu to set this up quickly using LXD virtual machines. See:

jrd10 commented

Thank you for this quick and complete answer.

I will try, give me the time to do that :). I will provide you feedback.

Many thanks for your time and interest, José

jgneff commented

Please start a discussion if you encounter any other problems.