Receiving “wrong name” NoClassDefFoundError when executing a Java program from the command-line

百般思念 提交于 2019-11-26 16:35:57

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

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;

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

Try using:

java es_2011.ProgAudioJ

(instead of java ProgAudioJ).

I'm making some assumptions here about your current working directory and your CLASSPATH. If you can provide information about the command you're running (e.g. what directory you're in, where the class file is located, etc.), we can help you more efficiently.

Try this (compile and run):

dir

2011-02-10  00:30    <DIR>          .
2011-02-10  00:30    <DIR>          ..
2011-02-10  00:27                58 es_2011

javac es_2011/ProgAudioJ

java es_2011.ProgAudioJ

It's quite clearly stated there:

java.lang.NoClassDefFoundError: ProgAudioJ (wrong name: es_2011/ProgAudioJ)

If you want to put a class in a package(*), then the source code must be placed in a corresponding directory, e.g.,

src/Main.java <- root package (no declaration)
src/es_2011/ProgAudioJ.java <- package es_2011;

(*) You should do it always, except for tiny throw-away stuff and possibly for the main class.

Amar

Try this,

  1. Compile your class using below command  

    $ javac ProgAudioJ.java -d .
    
  2. Run your application by command

    $ java es_2011.ProgAudioJ
    

The reason that it works when you remove
package es_2011
is that you are changing how the compiler packages up, and effectively locates, the file.

I had the same problem - and the error message wrong name: does indeed point you to the answer. You are using the wrong name "ProgAudioJ" in order to run the .class file. It has been packaged up as

es_2011/ProgAudioJ

In order to run it - you have to either move up a directory:

If you are here: (Windows) src\es_2011\
move to src\

Then run the line:

java es_2011.ProgAudioJ

This tells the compiler to look for the ProgAudioJ - which resides in the es_2011 package. For a standard installation, this will be based on folders - so it will look for the es_2011 folder first, and then the name of the .class file that you want to run (ProgAudio).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!