0

I have a JavaFX application that works fine when I run it inside IntelliJ IDEA, but when I try to build it into an executable JAR file, I get this error:

Exception in thread “main” java.lang.NoClassDefFoundError: javafx/application/Application
 at java.base/java.lang.ClassLoader.defineClass1(Native Method)
 at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1013)
 at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
 at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:862)
 at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:760)
 at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681)
 at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:639)
 at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
 at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
 at com.example.song_finder_fx.App.main(App.java:5)
Caused by: java.lang.ClassNotFoundException:  javafx.application.Application
 at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
 at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
 at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) … 10 more

I have searched many resources and found that this error is related to some JAR files from JavaFX SDK (‘javafx.controls’, ‘javafx.fxml’). But I have added all the used libraries to the artifact when building the JAR file, including these two JAR files.

I am using Java SDK 19, JavaFX 21-ea+24 with IntelliJ IDEA.

3
  • Packaging JavaFX applications is not that simple, see github.com/dlemmermann/JPackageScriptFX. Commented Oct 19, 2023 at 15:48
  • Packaging info is in the javafx tag, also see openjfx info on runtime images. Also, don't use the ea distributions - those are early access versions of software that may be less stable and reliable than a standard (non-ea) distribution. Commented Oct 19, 2023 at 19:58
  • If your application is completely modular, then I advise creating a zip of your application using jlink (or jpackage, e.g. via JPackageScriptFX only if you need a more professional installer or have a non-modular project or automatic module dependencies). If you use the Idea new JavaFX project wizard and run the maven javafx:jlink goal on the resultant project, it will create a redistributable image for you in a couple of seconds. Commented Oct 19, 2023 at 20:03

1 Answer 1

0

You must create a fat jar.

For modular application

You can create a JavaFX application with IntelliJ with the build management Tool Maven or Gradle. There is a Maven/Gradle command for creating the jar by default. Look how it is done there.

For non modular project

If you use Gradle (instead of Maven), you can take a look at this sample application on my GitHub:

https://github.com/davidweber411/SimpleJavaFxAppWithoutModules

There is a Gradle Task "createCustomFatJar" which creates the complete executable jar.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.