简体   繁体   English

运行jar时出现NoClassDefFound

[英]NoClassDefFound when running a jar

I built a jar using IntelliJ, setting the main class properly. 我使用IntelliJ构建了一个jar,正确设置了主类。

When I run "java -jar foo.jar" from the command line (Windows), I get an exception that claims the main file is missing. 当我从命令行(Windows)运行“ java -jar foo.jar”时,出现一个异常,声称缺少主文件。 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. 并且jar包含带有.class文件的适当目录结构。

If you do java -tf foo.jar , do you see something like this? 如果执行java -tf foo.jar ,会看到类似的内容吗?

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文件来确保Java知道主文件在其中:

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

You're trying to execute mypackage.LockUtil , but you should use mypackage.locking.LockUtil (note the package statement at the beginning of the class.). 您正在尝试执行mypackage.LockUtil ,但是您应该使用mypackage.locking.LockUtil (请注意在类开头的package语句。)。

Another possibility is that you have moved the class and forgot the update the package statement. 另一种可能性是您已经移动了类,却忘记了更新package语句。

LockUtil是否依赖于另一个无法解析的类,从而不允许LockUtil加载?

您的软件包的名称似乎是mypackage.locking,而不仅仅是mypackage

It appears that your main-class definition in your manifest is pointing at mypackage/LockUtil rather than mypackage/locking/LockUtil. 清单中的主类定义似乎指向mypackage / LockUtil,而不是mypackage / locking / LockUtil。

-Rick -Rick

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM