接收“错误名称”从命令行执行Java程序时的NoClassDefFoundError [英] Receiving "wrong name" NoClassDefFoundError when executing a Java program from the command-line

查看:132
本文介绍了接收“错误名称”从命令行执行Java程序时的NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试执行我的java应用程序时遇到问题。
每当我尝试通过命令执行程序时

I have a problem while trying executing my java application. Whenever I try to execute the program through the command

java ProgAudioJ

我收到此错误:

Exception in thread "main"
java.lang.NoClassDefFoundError: ProgAudioJ (wrong name: es_2011/ProgAudioJ)
        at java.lang.ClassLoader.defineClass1(NativeMethod)
        at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
        at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
        at java.security.AccessController.doPrivileged(NativeMethod)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: ProgAudioJ.  Program will exit.

如果我从我的代码中删除:

If I remove from my code:

package es_2011;

一切都很完美。我该如何解决这个问题?

Everything works perfectly. How do I solve the problem?

推荐答案

因为我发现这些答案不清楚,所以这就是你需要做的。
首先,如果你打包你的代码(IE你的类在顶部有 package 关键字),编译的类必须在一个同名的目录中。代码中的包声明。编译完类后,需要在执行java命令时向上移动目录,并且包含程序包的名称。例如,如果您的代码存在于 / myFolder / myPackage / 中,并且您的类以包myPackage 开头(请注意目录和包是相同的名称),然后你会做以下(linux / osx):

Because I found these answers unclear, here is what you need to do. First, if you package your code (IE your classes have the package keyword at the top) the compiled classes have to be in a directory with the same name as your package declaration in code. After you have compiled your classes, you need to move up a directory when you exectute the java command, and you include the name of the package. For example, if your code exists in /myFolder/myPackage/ , and your class starts with package myPackage (note that the directory and the package are the same name), then you would do the following (linux / osx):

cd /myFolder/myPackage

javac MyClass.java 

cd ..

java myPackage.MyClass

编辑 - 一个晚期编辑,以澄清我看到人们感到困惑的事情。在上面的例子中,包只有一个深,这意味着它只是myPackage。如果你的代码有一个更大的包,比如

Edit - A late edit to clarify something I see people get confused on. In the example above, the package is only one deep, meaning its just myPackage. If you code has a larger package, like

package com.somedomain.someproject;

您需要从包含该软件包根目录的目录中执行java命令。例如,如果您编译的代码在myCode / com / somedomain / someproject / MyMainClass.class中,那么您将从myCode文件夹执行java命令,如下所示(再次,请特别注意目录结构与包相同)声明):

you will need to execute the java command from the directory which contains the root directory for that package. For example if your compiled code is in myCode/com/somedomain/someproject/MyMainClass.class, then you will execute the java command from the myCode folder, like this (Again, take special note that the directory structure is the same as the package declaration):

cd /myCode
java com.somedomain.someproject.MyMainClass

这篇关于接收“错误名称”从命令行执行Java程序时的NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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