这个问题一直让我自己写的class类无法再hadoop平台运行,困惑好几天了,看权威指南的进度直接无进展。
解决方法是在 conf/hadoop-env.sh添加hadoop的类路径
# Extra Java CLASSPATH elements. Optional.
# export HADOOP_CLASSPATH=
export HADOOP_CLASSPATH=.
还有用javac编译hadoop平台的java代码要用到好多的hadoop的class类,一直写 javac -classpath ...
很麻烦,可以在 ~/.bashrc 中添加如下语句:
export CLASSPATH="$CLASSPATH:$HADOOP_HOME/hadoop-core-0.20.203.0.jar:$HADOOP_HOME/lib/commons-cli-1.2.jar"
------------------------------------------------------------------------------------
一下转载自:http://blog.163.com/yaoyiyan_3y/blog/static/186083580201223115636805/
第一个问题是:
当启动hadoop时,会报如下错误:
starting namenode, logging to /home/june/hadoop-0.21.0/bin/../logs/hadoop-root-namenode-june-bjtu.out
localhost: Error: JAVA_HOME is not set.
localhost: Error: JAVA_HOME is not set.
starting jobtracker, logging to /home/june/hadoop-0.21.0/bin/../logs/hadoop-root-jobtracker-june-bjtu.out
localhost: Error: JAVA_HOME is not set.
解决办法是:
在hadoop-0.21.0/conf/文件夹里有个hadoop-env.sh的文件,它的作用是配置与hadoop运行环境相关的变量,其中有一个变量是JAVA_HOME,将它修改为你安装java的路径。具体操作如下:
vim hadoop-env.sh
# The java implementation to use. Required.
# export JAVA_HOME=/usr/lib/j2sdk1.6-sun
export JAVA_HOME=/usr/lib/jvm/java-6-openjdk
第二个问题是:
执行java程序时要先编译成class文件,才能用hadoop命令,但是编译的时候会有如下错误:
javac FileSystemCat.java
FileSystemCat.java:3: package org.apache.hadoop.io does not exist
import org.apache.hadoop.io.*;
^
FileSystemCat.java:4: package org.apache.hadoop.conf does not exist
import org.apache.hadoop.conf.*;
^
FileSystemCat.java:5: package org.apache.hadoop.fs does not exist
import org.apache.hadoop.fs.*;
解决办法是:
这是一个纯java问题啦,具体解决办法有:
javac -classpath "/home/june/hadoop-0.21.0/hadoop-common-0.21.0.jar" FileSystemCat.java
或者
export CLASSPATH=.:/home/june/hadoop-0.21.0/hadoop-common-0.21.0.jar:$CLASSPATH
还有就是,书上说的程序没有说那些类所在的包,所以自己要查查API啦。
第三个问题是:
执行读文件的程序时会报如下错误:
hadoop FileSystemCat hdfs://localhost/user/june/quangle.txt
Exception in thread "main" java.lang.NoClassDefFoundError: FileSystemCat
Caused by: java.lang.ClassNotFoundException: FileSystemCat
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: FileSystemCat. Program will exit.
解决办法是:
熟悉java的人肯定知道是因为没有设置classpath,在hadoop环境下怎么设置classpath呢?同样也是修改hadoop-0.21.0/conf/文件夹里的hadoop-env.sh文件,具体操作如下:
# Extra Java CLASSPATH elements. Optional.
# export HADOOP_CLASSPATH=
export HADOOP_CLASSPATH=.