NoClassDefFound when starting a jar

I built the jar using IntelliJ, setting the main class correctly.

When I run "java -jar foo.jar" from the command line (Windows), I get an exception that states that the main file is missing. The main class looks something like this:

package mypackage;

public class LockUtil {
  public static void main(String[] args) {
  ...

I get the following exception:

Exception in thread "main" java.lang.NoClassDefFoundError: mypackage/LockUtil
Caused by: java.lang.ClassNotFoundException: mypackage.LockUtil
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: mypackage.LockUtil. Program will exit.

The manifest file contains:

Manifest-Version: 1.0
Created-By: IntelliJ IDEA
Main-Class: mypackage.LockUtil

And jar contains the corresponding directory structure with the .class file.

0
source share
5 answers

If you do java -tf foo.jar, do you see something like this?

META-INF/
META-INF/MANIFEST.MF
mypackage/
mypackage/LockUtil.class

Could it be that somewhere there is a different directory level?

You can be sure that Java knows that the main file exists by creating a jar file like this:

jar cfe foo.jar mypackage.LockUtil mypackage/LockUtil.class
+2

mypackage.LockUtil, mypackage.locking.LockUtil ( package .).

, package.

+1

LockUtil , , LockUtil ?

+1

, - mypackage.locking, mypackage

0

, mypackage/LockUtil, mypackage/locking/LockUtil.

-Rick

0

Source: https://habr.com/ru/post/1717613/


All Articles