Search code examples
javajar

Getting Error: Could not find or load main class while opening executable Jar file


I'm opening fontawesomefx-glyphsbrowser-all-1.0.jar
Using java -jar /home/ubuntu/Downloads/fontawesomefx-glyphsbrowser-all-1.0.jar but getting error

Could not find or load main class

Also tried :

 java -cp /home/ubuntu/Downloads/fontawesomefx-glyphsbrowser-1.3.0/lib/fontawesomefx-glyphsbrowser-1.3.0.jar de.jensd.fx.glyphs.browser.GlyphsBrowser  

and getting:
Error: Could not find or load main class de.jensd.fx.glyphs.browser.GlyphsBrowser Caused by: java.lang.NoClassDefFoundError: javafx/scene/layout/VBox
Another try:

java -jar fontawesomefx-glyphsbrowser-all-1.0.jar
Error: Could not find or load main class de.jensd.fx.glyphs.browser.GlyphsBrowserApp
Caused by: java.lang.NoClassDefFoundError: javafx/application/Application

JavaFX is no longer packaged with JDK but I think it is from JDK 11 and I'm using JDK 8 so why I'm getting Caused by: java.lang.NoClassDefFoundError: javafx/application/Application I'm using open-jdk 8.0

So how to do?


Solution

  • The 'main class not found' error comes in 2 cases:

    1. Manifest.MF file of the .jar package does not contain a valid 'Main-Class' attribute. This can happen due to the .jar file not being created correctly. This is mostly a bug with the creation of jar file.

    2. The Jar file is meant to be used as a library, but not as an executable program and therefore, it does not contain any class with 'main' method.

    For case # 1 (bug in manifest.mf), these are the possible workarounds / fixes:

    1. Ask the provider of jar file to create it again with proper 'Main-Class' definition in the manifest.mf file

    2. Extract the contents of jar file to a directory, add 'Main-Class' attribute to manifest.mf file and package the folder into jar again.

    Here is an article from Oracle on how the manifest.mf file can be updated:

    https://docs.oracle.com/javase/tutorial/deployment/jar/manifestindex.html