0

I have this Problem. When i run my code in Intellij it works fine, but if i do an artifact and build the jar, it doesnt work. I think its caused by an extern library. Heres my output:

Exception in thread "main" java.lang.NoClassDefFoundError: com/mindfusion/scheduling/Calendar
        at GUI.<init>(GUI.java:75)
        at Logfiles.main(Logfiles.java:13)
Caused by: java.lang.ClassNotFoundException: com.mindfusion.scheduling.Calendar
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        ... 2 more

I know which Class it is but i dont know how to solve the Problem. Im really just a beginner. Could you please help me and explain it simple. Thank you

Edit:

After i build the artifact with extracted Libraries this Error comes : Error: A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes

GottaGaming
  • 51
  • 1
  • 10
  • 1
    Can you show line 75 of `GUI.java`? Can you open the JAR as zip file and check if `com/mindfusion/scheduling/Calendar` exists? – dan1st Jan 15 '21 at 06:32
  • com/mindfusion/scheduling/Calendar exists in the jar. Here is Code-Line 75: protected CalendarWindow calendarWindow = new CalendarWindow(); – GottaGaming Jan 18 '21 at 05:39
  • 1
    How did you create the JAR? – dan1st Jan 18 '21 at 08:23
  • ive done an artifact with non extracted libraries and build the artifact. ive heard if the extern libraries are extracted this error could happen – GottaGaming Jan 18 '21 at 09:14
  • 1
    Yes, it might work with extracting libraries. – dan1st Jan 18 '21 at 12:10
  • Now there is a new Error : Error: A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes – GottaGaming Jan 19 '21 at 05:34
  • how did you solve this issue? – BeHappy May 16 '21 at 09:58
  • i just throw the causing library away and used another one – GottaGaming Jun 08 '21 at 04:35
  • @GottaGaming how did u solve this? I know it's 2022 but I am encountering the same issue, I already added the jar file in build path, and the file is really there but I am still encountering NoClassDefFoundError. I inspected the class that is having this error but it is just simply a getter and setter file. – J.Doe Apr 28 '22 at 01:28

2 Answers2

1

This error simply means the class file is not present in the jar.

One possible solution is you can download jd-gui which is used to look at jars. You can use this to check if the class is present.

Another solution is you can grep search the class in the jar with this simple command.

grep -l "<class-name>" <jar-name>.jar

if the class is not present in the jar file. you can add the class using jar command.

jar -cvf <jar-absolute-location> <class-path>
eg : jar -cvf GUI.jar com.mindfusion.scheduling.Calendar
sachin
  • 777
  • 5
  • 10
0

The easiest way to understand this issue, it to read the Javadoc for that class. From the Javadoc:

Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.

That means that NoClassDefFoundError can be thrown when that particular class is present during compile time but somehow not available during runtime. This could be due to missing JAR file, permission issues, or incorrect classpath on runtime.

Normally I see these issues when developers neglect to define the classpath for used libraries. They forget that your IDE has its own file defining the classpath (i.e. Eclipse has the .classpath file) so running the application from the IDE works fine (class is present during compile time), but after the application is compiled and the classpath is not defined in the machine hosting the application, NoClassDefFoundError is thrown (class "missing" at runtime).

My suggestion is figured out first if the classpath is correct. More times than none, this is the issue. If the classpath is correct, make sure all permissions are set correctly.

hfontanez
  • 5,774
  • 2
  • 25
  • 37
  • 1
    Hi, I know this is an old question, but I am going through the same issue. How do I figure out if the classpath is correct? Could you give a suggestion of how to go about solving the problem? – AntonyMN Oct 07 '21 at 15:57
  • @AntonyMN Your question is vague. But this is my best shot at answering. You have to define your dependencies. For example, if you are using Maven and you want to import libraries, you declare them in your POM. If you are referring classes that reside in another JAR, follow this: https://stackoverflow.com/questions/2910115/include-external-jar-when-running-java-jar. If they are internal, then you import them in the matter prescribed by your build tool (Ant, Maven, Gradle, etc.). If you downvoted my answer because you didn't understand, that was not cool. – hfontanez Oct 07 '21 at 16:40
  • Hi, I know it's 2022 but I am encountering the same issue, I already added the jar file in build path, and the file is really there but I am still encountering NoClassDefFoundError. I inspected the class that is having this error but it is just simply a getter and setter file. – J.Doe Apr 28 '22 at 01:24
  • @J.Doe how and where did you add the JAR file in the build path? – hfontanez Apr 28 '22 at 03:57
  • 1
    I have fixed this by adding the jar file in WEB-INF/lib aside from the build path. Thanks anyway. – J.Doe Apr 28 '22 at 04:59