<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Exception in thread &amp;quot;main&amp;quot; java.lang.ClassNotFoundException in Archives of Support Questions (Read Only)</title>
    <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Exception-in-thread-quot-main-quot-java-lang/m-p/98256#M11758</link>
    <description>&lt;P&gt;I am trying to run a MapReduce job using(using new API) on Hadoop 2.7.1 using command line. &lt;/P&gt;&lt;P&gt;I have followed the below steps.&lt;/P&gt;&lt;PRE&gt;javac -cp `hadoop classpath`MaxTemperatureWithCompression.java -d /Users/gangadharkadam/hadoopdata/build
jar -cvf MaxTemperatureWithCompression.jar /Users/gangadharkadam/hadoopdata/build
hadoop jar MaxTemperatureWithCompression.jar org.myorg.MaxTemperatureWithCompression user/ncdc/input /user/ncdc/output&lt;/PRE&gt;&lt;P&gt;No error in compiling and creating a jar file. But on execution I am gettign the folowing error&lt;/P&gt;&lt;PRE&gt;Error Messages- Exception in thread "main" java.lang.ClassNotFoundException: org.myorg.MaxTemperatureWithCompression at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:425) at java.lang.ClassLoader.loadClass(ClassLoader.java:358) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:274) at org.apache.hadoop.util.RunJar.run(RunJar.java:214) at org.apache.hadoop.util.RunJar.main(RunJar.java:136)
&lt;/PRE&gt;&lt;PRE&gt;Java Code-
package org.myorg;

//Standard Java Classes
import java.io.IOException;
import java.util.regex.Pattern;

//extends the class Configured, and implements the Tool utility class
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.apache.hadoop.util.GenericOptionsParser;

//send debugging messages from inside the mapper and reducer classes
import org.apache.log4j.Logger;

//Job class in order to create, configure, and run an instance of your MapReduce
import org.apache.hadoop.mapreduce.Job;

//extend the Mapper class with your own Map class and add your own processing instructions
import org.apache.hadoop.mapreduce.Mapper;

//extend it to create and customize your own Reduce class
import org.apache.hadoop.mapreduce.Reducer;

//Path class to access files in HDFS
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FileSystem;

//pass required paths using the FileInputFormat and FileOutputFormat classes
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

//Writable objects for writing, reading,and comparing values during map and reduce processing
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.compress.GzipCodec;

public class MaxTemperatureWithCompression extends Configured implements Tool {
    private static final Logger LOG = Logger.getLogger(MaxTemperatureWithCompression.class);

    //main menhod to invoke the toolrunner to create instance of MaxTemperatureWithCompression
    public static void main(String[] args) throws Exception {

        int res = ToolRunner.run(new MaxTemperatureWithCompression(), args);
        System.exit(res);

    }

    //call the run method to configure the job
    public int run(String[] args) throws Exception {
        if (args.length != 2) {
            System.err.println("Usage: MaxTemperatureWithCompression &amp;lt;input path&amp;gt; " + "&amp;lt;output path&amp;gt;");
            System.exit(-1);
          }

        Job job = Job.getInstance(getConf(), "MaxTemperatureWithCompression");

        //set the jar to use based on the class
        job.setJarByClass(MaxTemperatureWithCompression.class);

        //set the input and output path
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        //set the output key and value
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        //set the compressionformat     
        /*[*/FileOutputFormat.setCompressOutput(job, true);
            FileOutputFormat.setOutputCompressorClass(job, GzipCodec.class);/*]*/

        //set the mapper and reducer class
        job.setMapperClass(Map.class);
        job.setCombinerClass(Reduce.class);
        job.setReducerClass(Reduce.class);

        return job.waitForCompletion(true) ? 0 : 1;

    }

    //mapper
    public static class Map extends Mapper&amp;lt;LongWritable, Text, Text, IntWritable&amp;gt; {
        private static final int MISSING = 9999;

        @Override
        public void map(LongWritable key, Text value, Context context)
        throws IOException,InterruptedException {

            String line = value.toString();
            String year = line.substring(15,19);

            int airTemperature;

            if (line.charAt(87) == '+') {
                airTemperature = Integer.parseInt(line.substring(88, 92));
            }
            else {
                airTemperature = Integer.parseInt(line.substring(87, 92));
            }
            String quality = line.substring(92,93);

            if (airTemperature != MISSING &amp;amp;&amp;amp; quality.matches("[01459]")) {
                context.write(new Text(year), new IntWritable(airTemperature));
            }
        }

    }

    //reducer
    public static class Reduce extends Reducer&amp;lt;Text, IntWritable, Text, IntWritable&amp;gt; {

        @Override
        public void reduce(Text key, Iterable&amp;lt;IntWritable&amp;gt; values, Context context) 
        throws IOException, InterruptedException {
            int maxValue = Integer.MIN_VALUE;
            for (IntWritable value : values) {
                maxValue = Math.max(maxValue, value.get());
            }
            context.write(key, new IntWritable(maxValue));
        }

    }
}
&lt;/PRE&gt;&lt;P&gt;I checked jar file and the folder structure org/myorg/MaxTemperatureWithCompression.class   is present. What could be the reason for this error. Any help in resolving this is highly apprciated. Thanks.
&lt;/P&gt;</description>
    <pubDate>Wed, 09 Dec 2015 23:41:56 GMT</pubDate>
    <dc:creator>GeeKay2015</dc:creator>
    <dc:date>2015-12-09T23:41:56Z</dc:date>
    <item>
      <title>Exception in thread "main" java.lang.ClassNotFoundException</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Exception-in-thread-quot-main-quot-java-lang/m-p/98256#M11758</link>
      <description>&lt;P&gt;I am trying to run a MapReduce job using(using new API) on Hadoop 2.7.1 using command line. &lt;/P&gt;&lt;P&gt;I have followed the below steps.&lt;/P&gt;&lt;PRE&gt;javac -cp `hadoop classpath`MaxTemperatureWithCompression.java -d /Users/gangadharkadam/hadoopdata/build
jar -cvf MaxTemperatureWithCompression.jar /Users/gangadharkadam/hadoopdata/build
hadoop jar MaxTemperatureWithCompression.jar org.myorg.MaxTemperatureWithCompression user/ncdc/input /user/ncdc/output&lt;/PRE&gt;&lt;P&gt;No error in compiling and creating a jar file. But on execution I am gettign the folowing error&lt;/P&gt;&lt;PRE&gt;Error Messages- Exception in thread "main" java.lang.ClassNotFoundException: org.myorg.MaxTemperatureWithCompression at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:425) at java.lang.ClassLoader.loadClass(ClassLoader.java:358) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:274) at org.apache.hadoop.util.RunJar.run(RunJar.java:214) at org.apache.hadoop.util.RunJar.main(RunJar.java:136)
&lt;/PRE&gt;&lt;PRE&gt;Java Code-
package org.myorg;

//Standard Java Classes
import java.io.IOException;
import java.util.regex.Pattern;

//extends the class Configured, and implements the Tool utility class
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import org.apache.hadoop.util.GenericOptionsParser;

//send debugging messages from inside the mapper and reducer classes
import org.apache.log4j.Logger;

//Job class in order to create, configure, and run an instance of your MapReduce
import org.apache.hadoop.mapreduce.Job;

//extend the Mapper class with your own Map class and add your own processing instructions
import org.apache.hadoop.mapreduce.Mapper;

//extend it to create and customize your own Reduce class
import org.apache.hadoop.mapreduce.Reducer;

//Path class to access files in HDFS
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FileSystem;

//pass required paths using the FileInputFormat and FileOutputFormat classes
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

//Writable objects for writing, reading,and comparing values during map and reduce processing
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.compress.GzipCodec;

public class MaxTemperatureWithCompression extends Configured implements Tool {
    private static final Logger LOG = Logger.getLogger(MaxTemperatureWithCompression.class);

    //main menhod to invoke the toolrunner to create instance of MaxTemperatureWithCompression
    public static void main(String[] args) throws Exception {

        int res = ToolRunner.run(new MaxTemperatureWithCompression(), args);
        System.exit(res);

    }

    //call the run method to configure the job
    public int run(String[] args) throws Exception {
        if (args.length != 2) {
            System.err.println("Usage: MaxTemperatureWithCompression &amp;lt;input path&amp;gt; " + "&amp;lt;output path&amp;gt;");
            System.exit(-1);
          }

        Job job = Job.getInstance(getConf(), "MaxTemperatureWithCompression");

        //set the jar to use based on the class
        job.setJarByClass(MaxTemperatureWithCompression.class);

        //set the input and output path
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        //set the output key and value
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        //set the compressionformat     
        /*[*/FileOutputFormat.setCompressOutput(job, true);
            FileOutputFormat.setOutputCompressorClass(job, GzipCodec.class);/*]*/

        //set the mapper and reducer class
        job.setMapperClass(Map.class);
        job.setCombinerClass(Reduce.class);
        job.setReducerClass(Reduce.class);

        return job.waitForCompletion(true) ? 0 : 1;

    }

    //mapper
    public static class Map extends Mapper&amp;lt;LongWritable, Text, Text, IntWritable&amp;gt; {
        private static final int MISSING = 9999;

        @Override
        public void map(LongWritable key, Text value, Context context)
        throws IOException,InterruptedException {

            String line = value.toString();
            String year = line.substring(15,19);

            int airTemperature;

            if (line.charAt(87) == '+') {
                airTemperature = Integer.parseInt(line.substring(88, 92));
            }
            else {
                airTemperature = Integer.parseInt(line.substring(87, 92));
            }
            String quality = line.substring(92,93);

            if (airTemperature != MISSING &amp;amp;&amp;amp; quality.matches("[01459]")) {
                context.write(new Text(year), new IntWritable(airTemperature));
            }
        }

    }

    //reducer
    public static class Reduce extends Reducer&amp;lt;Text, IntWritable, Text, IntWritable&amp;gt; {

        @Override
        public void reduce(Text key, Iterable&amp;lt;IntWritable&amp;gt; values, Context context) 
        throws IOException, InterruptedException {
            int maxValue = Integer.MIN_VALUE;
            for (IntWritable value : values) {
                maxValue = Math.max(maxValue, value.get());
            }
            context.write(key, new IntWritable(maxValue));
        }

    }
}
&lt;/PRE&gt;&lt;P&gt;I checked jar file and the folder structure org/myorg/MaxTemperatureWithCompression.class   is present. What could be the reason for this error. Any help in resolving this is highly apprciated. Thanks.
&lt;/P&gt;</description>
      <pubDate>Wed, 09 Dec 2015 23:41:56 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Exception-in-thread-quot-main-quot-java-lang/m-p/98256#M11758</guid>
      <dc:creator>GeeKay2015</dc:creator>
      <dc:date>2015-12-09T23:41:56Z</dc:date>
    </item>
    <item>
      <title>Re: Exception in thread "main" java.lang.ClassNotFoundException</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Exception-in-thread-quot-main-quot-java-lang/m-p/98257#M11759</link>
      <description>&lt;P&gt;Hi &lt;A rel="user" href="https://community.cloudera.com/users/971/gkadam2011.html" nodeid="971"&gt;@Gangadhar Kadam&lt;/A&gt;,&lt;/P&gt;&lt;P&gt;You've got eveything almost right. When you build the jar, you need to move into the build directory and then trigger the jar -cvzf command to avoid having the "build part of the directory hierachy put into the JAR. So, the following should work: &lt;/P&gt;&lt;PRE&gt;javac -cp `hadoop classpath`MaxTemperatureWithCompression.java -d /Users/gangadharkadam/hadoopdata/build

cd /Users/gangadharkadam/hadoopdata/build 

jar -cvf MaxTemperatureWithCompression.jar . 

hadoop jar MaxTemperatureWithCompression.jar org.myorg.MaxTemperatureWithCompression user/ncdc/input /user/ncdc/output&lt;/PRE&gt;&lt;P&gt;Try it out and compare the results of jar -tf MaxTemperatureWithCompression.jar. You should see:&lt;/P&gt;&lt;PRE&gt;[root@sandbox build]# jar -tf MaxTemperatureWithCompression.jar
META-INF/
META-INF/MANIFEST.MF
org/
org/myorg/
org/myorg/MaxTemperatureWithCompression.class
org/myorg/MaxTemperatureWithCompression$Map.class
org/myorg/MaxTemperatureWithCompression$Reduce.class&lt;/PRE&gt;&lt;P&gt;Whereas currently your steps result in:&lt;/P&gt;&lt;PRE&gt;[root@sandbox test]# jar -tf MaxTemperatureWithCompression.jar
META-INF/
META-INF/MANIFEST.MF
build/org/
build/org/myorg/
build/org/myorg/MaxTemperatureWithCompression.class
build/org/myorg/MaxTemperatureWithCompression$Map.class
build/org/myorg/MaxTemperatureWithCompression$Reduce.class&lt;/PRE&gt;&lt;P&gt;This works for me on my HDP 2.3 Sandbox.&lt;/P&gt;</description>
      <pubDate>Thu, 10 Dec 2015 11:35:38 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Exception-in-thread-quot-main-quot-java-lang/m-p/98257#M11759</guid>
      <dc:creator>bwilson</dc:creator>
      <dc:date>2015-12-10T11:35:38Z</dc:date>
    </item>
    <item>
      <title>Re: Exception in thread "main" java.lang.ClassNotFoundException</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Exception-in-thread-quot-main-quot-java-lang/m-p/98258#M11760</link>
      <description>&lt;P&gt;Thanks Brandon.It worked.&lt;/P&gt;</description>
      <pubDate>Thu, 10 Dec 2015 12:34:19 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Exception-in-thread-quot-main-quot-java-lang/m-p/98258#M11760</guid>
      <dc:creator>GeeKay2015</dc:creator>
      <dc:date>2015-12-10T12:34:19Z</dc:date>
    </item>
  </channel>
</rss>

