I'm totally new to Java and I'm trying to build a Hadoop MapReduce program. I added /usr/lib/hadoop/hadoop-core.jar
and everything builds fine. When I export the project as a jar file
and try to run it, it gives me a ClassNotFoundException
when I run it because it doesn't not where to find the hadoop-core.jar
. How do I resolve this?
One way is to add a reference to the required resource into the manifest file of the main Jar. The references are relative, so the most common practice is to ensure the main and dependent Jars are in the same directory.
If deploying as an applet the Jar can be referenced in the archive attribute of the applet element.
If deploying using Java Web Start, the Jar can be referenced in the JNLP launch file.
Edit 1: Example of manifest presuming the dependent Jar is in the 'hadoop' sub-directory of the directory where the app. is located.
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Main-Class: org.pscode.wigglyworms.WigglyWorms
Class-Path: hadoop/hadoop-core.jar
Implementation-Title: Wiggly Worms
Implementation-Version: 11.03.13
Implementation-Vendor: PSCode - Andrew Thompson
Implementation-Vendor-Id: org.pscode
Created-By: 1.6.0-b105 (Sun Microsystems Inc.)
Note that a manifest file should end with a single blank line (which is difficult to represent even using the 'pre-formatted' mode for code). For that reason I would strongly recommend you leave the creation of the manifest to a tool that knows how to write them. My choice is the Ant build tool.
For further details, see Adding Classes to the JAR File's Classpath section of the 'Packaging Programs in JAR Files' lesson of the Java Tutorial.
这应该对Java应用程序启动器有帮助
The jar command won't include the hadoop-core.jar
inside the jar you built. So at runtime (ie when you execute the java
command) you need to specify hadoop on your classpath. Check out the -cp
or -classpath
command line argument. Basically it will look something like this:
java -cp myjar.jar;hadoop-core.jar package.Main
When you run the jar file you built, it needs to have a CLASSPATH environmental variable which contains the full path to /usr/lib/hadoop/hadoop-core.jar it might look something like this
set CLASSPATH=/usr/lib/hadoop/hadoop-core.jar
export CLASSPATH
Other techniques exist, but the basic idea is that your JVM knows where the classes are you built, but it has no idea where to look for the hadoop-core classes.
最简单的方法是使用“文件”->“导出”->“可运行的jar”,它提供了几种执行所需操作的选项。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.