运行jar时NoClassDefFound [英] NoClassDefFound when running a jar

查看:2710
本文介绍了运行jar时NoClassDefFound的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用IntelliJ构建了一个jar,正确设置了主类。

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

当我从命令行(Windows)运行java -jar foo.jar时,我得到一个声称主文件丢失的异常。主类类似于:

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) {
  ...

我收到以下异常:

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.

清单文件包含:

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

并且jar包含带有.class文件的相应目录结构。

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

推荐答案

如果你做了 java -tf foo.jar ,你会看到类似的东西吗?

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 there is another directory level in there somewhere?

你可以肯定Java知道主文件是通过使用以下内容构建jar文件:

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

这篇关于运行jar时NoClassDefFound的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆