javahadoopmapreduce

Unable to run hadoop application due to NoClassDefFoundError


I am new in Hadoop and I have been following some tutorials like this. I have found a nice set of mapreduce examples in here.

I was able to run the "wordcount" example, but I am not able to run the "EnhancedTopN" example. It gives me the error:

Exception in thread "main" java.lang.NoClassDefFoundError: EnhancedTopN (wrong name: samples/topn_enhanced/EnhancedTopN)

I have correctly compiled the java file, although it gives me a note saying "Note: EnhancedTopN.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.". What could be wrong?


Solution

  • NoClassDefFoundError with the message "wrong name" means that the package structure of your class is wrong, or that you are running it the wrong way. This has nothing to do with Hadoop, just with the way packages work in Java.

    Is your class EnhancedTopN in a package named samples.topn_enhanced; do you have the following package statement at the top of the source file?

    package samples.topn_enhanced;
    
    public class EnhancedTopN {
        // ...
    }
    

    The directory structure of your project must match the package structure, so the source file EnhancedTopN.java should be in a directory samples\topn_enhanced, and you must compile and run it from the base directory of the package structure:

    C:\Project> javac samples\topn_enhanced\EnhancedTopN.java
    
    C:\Project> java samples.topn_enhanced.EnhancedTopN
    

    Going into the directory and using java EnhancedTopN will not work and will give you the error that you are asking about:

    C:\Project\samples\topn_enhanced> java EnhancedTopN    -- error!
    

    See: Lesson: Packages in Oracle's Java Tutorials.