Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

java ProgAudioJ

I get this error:

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?

like image 638
MAX Avatar asked Feb 09 '11 23:02

MAX


People also ask

How do I fix Java Lang NoClassDefFoundError error?

lang. NoClassDefFoundError, which means the Class Loader file responsible for dynamically loading classes can not find the . class file. So to remove this error, you should set your classpath to the location where your Class Loader is present.

Why am I getting a NoClassDefFoundError in Java?

The NoClassDefFoundError is a runtime error in Java that occurs if the Java Virtual Machine (JVM) or a ClassLoader instance attempts to load the definition of a class that could not be found. The class definition exists at compile-time but is not available at runtime.

Can we catch NoClassDefFoundError in Java?

In the case of NoClassDefFoundError, the class was present at compile time, but Java runtime could not find it in Java classpath during runtime.


2 Answers

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
like image 116
Mark W Avatar answered Sep 22 '22 07:09

Mark W


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.

like image 27
Rob Hruska Avatar answered Sep 24 '22 07:09

Rob Hruska