Home:ALL Converter>Getting Error when running hadoop command copyFromLocalFile in java

Getting Error when running hadoop command copyFromLocalFile in java

Ask Time:2019-01-17T14:13:36         Author:newbiecihuy

Json Formatter

I'm newbie in Hadoop cluster node. I try running Hadoop command in java

The following code is to copy file from local to HDFS.

When I use command shell, it is working:
hadoop fs -copyFromLocal /home/my_name/tutorial_append.txt /user/my_name/tutorial_append.txt

But when I implement it in java code like this:

import java.io.IOException;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;



  public class UploadTest {

    public static void main(String[] args) throws IOException {

         System.out.print("Hello World");
         String localPath="/home/my_name/tutorial_append.txt";

         String uri ="hdfs://0.0.0.0:8020";

         String hdfsdir="hdfs://0.0.0.0:8020/user/my_name/";

         Configuration conf = new Configuration();

         FileSystem fs =  FileSystem.get(URI.create(uri),conf);

         fs.copyFromLocalFile(new Path(localPath),new Path(hdfsdir));
    }
}

compile javac -cp hadoop-core-1.2.1.jar UploadTest.java

run java -cp hadoop-core-1.2.1.jar UploadTest

Error: Could not find or load main class UploadTest

I have tried it many times but I've been unsuccessful.

SOLVED

Finally I build to jar file's and I run using hadoop jar UploadTest.jar

I was following this Hadoop Error: Could not find or load main class class path TestJava

Author:newbiecihuy,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/54229997/getting-error-when-running-hadoop-command-copyfromlocalfile-in-java
yy