Home > Back-end >  JavaFX : Jar Could not find or load main class main.Launcher
JavaFX : Jar Could not find or load main class main.Launcher

Time:09-18

I wrote a JavaFX application with JavaFX version 15.0.1.

I'm using IntelliJ IDEA and all is working fine since I execute my code from IntelliJ.

Now I want to deploy the application building the Jar and creating an Executable.

I already know that there are some issues with versions after Java 8 because the packaging is not included anymore.

I searched a lot and found some useful stuff here on StackOverflow and on YouTube, following all the guides step by step.

I also found a video of a guy that uses my same JavaFX version, and IntelliJ to build the Jar, and at the end, he simply double-click on the created Jar and the application runs smoothly and with no errors.

That's my actual setup :

I set up the Launcher.java class to bypass the Main.java problem like in the guides.

package main;

import main.Main;

public class Launcher
{ 
    public static void main(String[] args){Main.main(args);}
}

I built the project and then in artifact I created a new one based on modules and added the .dll files from my JavaFX SDK.

Artifact Setup

After that, I built the project once again, and I went to : Build -> Build Artifacts and Build.

After this process, my out directory seems like this :

Out Directory

And in artifacts -> MenuManager_jar there is my MenuManager.jar file.

MenuManager.jar File

After that, as I saw in the guides, I can simply double click on it, or run it with : java -jar MenuManager.jar, and it should run my application.

But after I double click, nothing happens, even if I run from console, with this command :

java -jar MenuManager.jar

or even

java --module-path "C:\Program Files\Java\javafx-sdk-15.0.1\lib" --add-modules javafx.controls,javafx.fxml -jar MenuManager.jar

it shows this error

Error: Could not find or load main class main.Launcher
Caused by: java.lang.ClassNotFoundException: main.Launcher

So after that, I decided to look for the Launcher.class file myself.

And that is present in my Jar opened as zip :

Jar/main content

Obviously out of the "main" directory in the jar there are all the dependencies/libraries :

Jar Content

I looked for posts/videos/guides/packaging tools, but I can't find a way to create this Jar or Executable for my Project. This process creates the Jar in the right way, but it seems I'm not able to run it for some reason.

I need help with this last step.

CodePudding user response:

Found out myself the way to fix it!

I had some other external libraries imported in my project over the standard JavaFX jar.

It was a specific mysql library version imported as Jar.

It probably had some problems being built in artifact, I still don't know the reason, maybe it was a version problem or incompatibility.

After replacing it with another Library or removing it, the jar built fine and I'm able to run my appliation even double-clicking.

Suggestions

What I suggest to people with same problem or that want to start a JavaFX application and knows that they are going to create a jar is that :

  • After importing a new Library try to build the artifact so you won't have some suriprises after using it and building.
  • If the library is making the final jar not working, try to replace it with a similar one, or try to find out if there is a different version of the library.
  • Since the start of the project try to build and build artifact every time you make some big changes to be able to track the problem faster.
  • Related