se0D2
se0D2

Reputation: 11

Error while running JavaFX from terminal - Could not find or load main class

Need some clever advice to solve this - well, some kind of problem while using simple JavaFX app. However, there was some problems while all is set-up (module path, for example), but what now when I just cannot run .jar file from terminal? Ok, start talk about mission-impossible-with-javafx:

I got Apache NetBeans 12 installed on Linux Mint 20. JDK is already installed, so 'java -version' returns 'openjdk version "11.0.7" 2020-04-14'... When start project there is no problem at all - JavaFX app works.

I need this app to work on Windows 8.1, but before that I tried to run this .jar file on my linux terminal using java -jar MyFile.jar. Here the problem starts: it said no main manifest attribute, in MyFile.jar. Then I decide to set the main class directly in jar file (opened using Archive Manager): MyFile.jar -> META-INF -> MANIFEST.MF and add on last line: Main-Class: mypackage.MyClass

Now I have: Error: Could not find or load main class mypackage.MyClass Caused by: java.lang.NoClassDefFoundError: javafx/application/Application

Ok, how and why?

Just to said that in NetBeans I also set main class in: Project -> Properties -> Run -> Main class: mypackage.MyClass and in VM options I added: --module-path "/locationToJavaFX/javafx-sdk-11.0.2/lib" --add-modules javafx.controls,javafx.fxml

In pom file, main class is also set (generated by IDE):

//  other lines
<groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.4</version>
                <configuration>
                    <mainClass>mypackage.MyClass</mainClass>
                </configuration>
//  other lines

and module-info looks like:

module mypackage {
    requires javafx.controls;
    requires javafx.fxml;
    requires org.jsoup;
    
    opens mypackage to javafx.fxml;
    exports mypackage;      // some people advice without this line, I tried but same problem
}

Just want to be clear - I tried probably everything (I don't count) that is available here on stackoverflow. I try to fix this yesterday and today (almost 2 whole days) and I can't.

Just a thought: Java programming is not so difficult as configuration is. Cheers and thanks for helping!

EDIT: Just to said that I tried what José suggested : first creating launcher class, follow 'fat jar' step-by-step then run 'semi-fat jar' command which he added to the post. Interestingly enough, this return me: Error occurred during initialization of boot layer java.lang.module.FindException: Module javafx-fxml not found. Why not found javafx-fxml if I aready defined it as said above in question? Also, why Netbeans doesn't provide me the main class in manifest (in jar) file? When I add main class in manifest, I get this:

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:1017)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:800)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:698)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:621)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:579)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at Main.main(Main.java:5) Caused by: java.lang.ClassNotFoundException: javafx.application.Application
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 10 more

Ok, how Application does not found? :)

EDIT 2: Yeap, mister José has a right. This command in his 'semi-fat jar' post actually works BUT not for Maven (I use it on same Ant project and it works). Maven somehow cannot recognize JSoup class (even I add it in dependency) and java returns:

Exception in Application start method Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834) Caused by: java.lang.NoClassDefFoundError: org/jsoup/Jsoup
at p.eis.GetData1.startIt(GetData1.java:34)
at p.eis.GetData1.<init>(GetData1.java:28)
at p.eis.SampleController.initialize(SampleController.java:37)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2573)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
at p.eis.Start.loadFXML(Start.java:32)
at p.eis.Start.start(Start.java:19)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
... 1 more Caused by: java.lang.ClassNotFoundException: org.jsoup.Jsoup
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 17 more

Hope I'll find solution for Maven projects in the near future.

Upvotes: 1

Views: 2915

Answers (3)

Jacob Hilst
Jacob Hilst

Reputation: 1

Saw this issue myself and elsewhere, and found a solution through process of elimination. In configurations, inside the launch.json file:

  {
        "type": "java",
        "name": "HelloWorld",
        "request": "launch",
        "vmArgs": "--module-path **\"**PATH TO\\javafx-sdk-21.0.2\\lib\\**\"** --add-modules javafx.controls,javafx.fxml",
        "mainClass": "HelloWorld",
        "projectName": "TestFolder"
    },

Note the "" around the path! If the " remains, it's supposed to be bold.

For each additional file you'll have to add the vmArgs manually, but this works!

I'll be posting this elsewhere for other issues like this in hopes it helps others who had a rough patch with it.

Upvotes: 0

Hamid Shaikh
Hamid Shaikh

Reputation: 41

Yeah I noticed you use maven but it's not fault of maven, I know why this happen and i have solution about it so in my solution there is magic and work fine because i use my solution to make jar and you know my jar file run without terminal(means double clicking on jar file)

Solution

you need winRar because i use it for info read my solution!

Just follow below step with images to run jar(both maven and gradle project)

  1. solution of Error: Could not find or load main class mypackage. this line you created main class at this location /src/main/java/YOUR_PACKAGE_NAME/Main.java so while creating jar file intellij couldn't care your main class if you create main class at above location and it doesn't add Main-Class: Main this line in Manifest File Solution is that create one more main class(don't delete Main.Java class we use this class in new one main class) at this location /src/main/java/EntryPoint.java see below image and code what i did

EntryPoint.java is new main class (IMAGE)

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

}

Main.java Image

Main.Java (IMAGE)

  1. After creating EntryPoint.java(New Main Class) Manifest File For Jar File, clicking Cntr+Alt+Shift+S to create Manifest File | Jar File, during creating jar file(before build jar) Just select EntryPoint.Java As Main Class Below Image

Main Class : EntryPoint (IMAGE)

Before click Ok button change the location of manifest file create it at this location /src/META_INF/manifest.mf(Wrong location : /src/main/META_INF/menifest.mf) and click Apply button to save changes

Location Of Manifest File like below image

Just Manifest.MF File (IMAGE)

  1. Build Jar file go to Option[Build->Build-Artifacts...] and take 3-5 min to create jar.

  2. After Creating Jar file Please Read clearly this main step means Means Solution Of This line of NOT FOUND FXML FILE this is tricky because all fxml file are stored at resource directory so while creating jar so intellij doesn't add the any file from resource directory again not include fxml file while creating jar

Solution Of How to include FXML FILE NOW WE HAVE TO USE WIN_RAR Software To Add This File And Believe Me Its work and not affect any file and work file Just Follow below step to add FXML FILE

Adding FXML FILE STEP

  1. close Intellij
  2. Go to your project Directory in my case C:\Users\ADMIN\IdeaProjects\YOUR_PROJECT_NAME and Go to YOUR_PROJECT_NAME\out\artifacts\YOUR_PROJECT_NAME\YOUR_PROJECT_NAME.Jar so u find jar file of your project

Just click right key of Mouse for option, select option of EXTRACT HERE(WinRar Option)

JUST I AM ExtractING MY JAR (IMAGE)

DELETE ONLY JAR FILE AFTER EXTRACTING Like my file name smartSeizeing.jar i delete it (IMAGE)

  1. After extracting and deleting jar(only jar file) file so OPEN YOUR_PACKAGE_NAME File in My case C:\Users\ADMIN\IdeaProjects\SmartSeizeing\out\artifacts\SmartSeizeing_jar\smartSeizeing Keep Open It And Open Your Project directory and copy all file(including all file like fxml, css, html, hibernet file if u have other file to with fxml) from resource directory and Paste all file(copied Files) in that previously open folder(extract jar folder like IdeaProjects\YOUR_PROJECT_NAME\out\artifacts\YOUR_PROJECT_NAME_jar\YOUR_PACKAGE_NAME) And after coping file so and just make jar file select all file(IdeaProjects\YOUR_PROJECT_NAME\out\artifacts\YOUR_PROJECT_NAME_jar) and click right key of mouse, select add to archive(WinRar) and after creating YOUR_PACKAGE_NAME.jar file just double click that file and see the magic

How to Add fxml and how to create again jar(without any curreption) i mentioned in below image so u can easily understand what i did and also image of my OUTPUT

i pasted all file of resource directory of my project (IMAGE)

Please see below answer too because i can't add more link for step of how to make jar using winRar and that jar work fine and also my output

Best Of Luck

and You wanna video just tell me i make video about it How to make runnable jar without any exception or error!

Upvotes: 0

Related Questions