Search code examples
javajsonhadoopmapreduceclassnotfoundexception

Getting "java.lang.ClassNotFoundException: org.json.simple.parser.ParseException" while reading JSON object as input value of mapper


I have written a mapper program with an intention to read JSON data. The code looks like this:

public void map(LongWritable key, Text value, Context context)
        throws IOException, InterruptedException {
    System.out.println("Into Map");
    try {
        String line = value.toString();
        JSONParser jsonParser = new JSONParser();
        JSONObject jsonObject = new JSONObject();
        jsonObject = (JSONObject) jsonParser.parse(line);
        String content = (String) jsonObject.get("content");
        System.out.println(content);
        StringTokenizer tokenizer = new StringTokenizer(content);
        while (tokenizer.hasMoreTokens()) {
            word.set(tokenizer.nextToken());
            String strword = word.toString();
                context.write(word, one);
        }
    } catch (ParseException ex) {
        ex.printStackTrace();
    }
}

However, when I try to run the mapper, I get following error:

Error: java.lang.ClassNotFoundException: org.json.simple.parser.ParseException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:249)
    at org.apache.hadoop.conf.Configuration.getClassByName(Configuration.java:810)
    at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:855)
    at org.apache.hadoop.mapreduce.JobContext.getMapperClass(JobContext.java:199)
    at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:718)
    at org.apache.hadoop.mapred.MapTask.run(MapTask.java:364)
    at org.apache.hadoop.mapred.Child$4.run(Child.java:255)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Subject.java:396)
    at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1190)
    at org.apache.hadoop.mapred.Child.main(Child.java:249)

Can someone point out what I am doing wrong?


Solution

  • It was a trivial mistake. I was not placing the jars in hadoop/lib folder. I had only placed them in the eclipse workspace.

    P.S. I had first deleted this post but then I saw someone else ask a similar question in another forum. Hence I undeleted the question and answered it. Also I have edited the question to highlight the actual cause.