Hadoop运行命令出现java.lang.ClassNotFoundException问题求助
Hey there, let's tackle that java.lang.ClassNotFoundException you're hitting with your Hadoop job. I've seen this pop up pretty often, and it almost always boils down to how you're packaging or referencing your job's classes. Let's go through the most common fixes step by step:
It's easy to accidentally package only source code or misconfigure the path structure. Run this command to inspect what's inside your JAR:
jar tf your-jar-file.jar
Look for .class files matching your Mapper, Reducer, and main job class. If you used a package declaration (like package com.my.hadoop;), the class files should be nested in a directory structure matching that package (e.g., com/my/hadoop/WordCount.class).
Hadoop needs to know which class to use as the entry point. If your main class is named WordCount (in the default package), run:
hadoop jar your-jar-file.jar WordCount input output
If you used a package, include the full qualified name:
hadoop jar your-jar-file.jar com.my.hadoop.WordCount input output
Alternatively, you can embed the main class info directly in the JAR's manifest:
- Create a
manifest.txtfile with this line (add a blank line at the end):Main-Class: WordCount - Repackage your JAR with:
jar cvfm your-jar-file.jar manifest.txt *.class
Now you can run the job without specifying the main class:
hadoop jar your-jar-file.jar input output
Mismatched versions or missing compile-time dependencies can cause class lookup failures. For manual compilation (no build tool), use Hadoop's own classpath to compile:
javac -classpath $(hadoop classpath) WordCount.java -d build/
Then package the compiled classes from the build directory (this preserves package structure):
jar cvf your-jar-file.jar -C build/ .
If you use Maven, make sure your pom.xml references Hadoop 3.0.0 with provided scope (since Hadoop provides these libs at runtime):
<dependencies> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>3.0.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-mapreduce-client-core</artifactId> <version>3.0.0</version> <scope>provided</scope> </dependency> </dependencies>
If your code starts with a package statement, your compiled class files must live in a directory structure that mirrors that package. For example, package org.example; requires the class file to be in org/example/ inside the JAR. If you skip this structure, Hadoop won't find the class even if it's in the JAR.
If you still run into issues, share the full error output and the result of jar tf your-jar-file.jar—that'll help narrow down the exact problem.
内容的提问来源于stack exchange,提问作者Tim

