ripper234
ripper234

Reputation: 230038

NoClassDefFound when running a jar

I built a jar using IntelliJ, setting the main class properly.

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

package mypackage;

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

I'm getting 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 the jar contains the appropriate directory structure with the .class file.

Upvotes: 0

Views: 6111

Answers (5)

lavinio
lavinio

Reputation: 24309

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

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

Could it be that there is another directory level in there somewhere?

You can be sure that Java knows the main file is there by building the jar file with something like this:

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

Upvotes: 2

Robin
Robin

Reputation: 24262

Does LockUtil have a dependency on another class that is not resolvable, thereby not allowing LockUtil to load?

Upvotes: 1

Rick
Rick

Reputation: 3840

It appears that your main-class definition in your manifest is pointing at mypackage/LockUtil rather than mypackage/locking/LockUtil.

-Rick

Upvotes: 0

LB40
LB40

Reputation: 12331

It seems that the name of your package is mypackage.locking and not only mypackage

Upvotes: 0

andri
andri

Reputation: 11292

You're trying to execute mypackage.LockUtil, but you should use mypackage.locking.LockUtil (note the package statement at the beginning of the class.).

Another possibility is that you have moved the class and forgot the update the package statement.

Upvotes: 1

Related Questions