0

I am trying to figure out how to use the JSONreader package, I have downloaded the gson-2.8.6.jar file and am using the command: javac -classpath gson-2.8.6.jar Dummy.java

This command works fine but when I go to execute the file with JSONreader within it, I get this error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gson/stream/JsonReader
        at Dummy.main(Dummy.java:14)
Caused by: java.lang.ClassNotFoundException: com.google.gson.stream.JsonReader
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:351)

My Dummy.java looks like this:

import java.net.*;
import java.io.*;
import java.sql.*;
import java.util.*;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;

public class Dummy {
    public static void main(String[] args){
        String json = "{\"brand\" : \"Toyota\", \"doors\" : 5}";

        JsonReader jsonReader = new JsonReader(new StringReader(json));
        try {
            while(jsonReader.hasNext()){
                JsonToken nextToken = jsonReader.peek();
                System.out.println(nextToken);
        
                if(JsonToken.BEGIN_OBJECT.equals(nextToken)){
        
                    jsonReader.beginObject();
        
                } else if(JsonToken.NAME.equals(nextToken)){
        
                    String name  =  jsonReader.nextName();
                    System.out.println(name);
        
                } else if(JsonToken.STRING.equals(nextToken)){
        
                    String value =  jsonReader.nextString();
                    System.out.println(value);
        
                } else if(JsonToken.NUMBER.equals(nextToken)){
        
                    long value =  jsonReader.nextLong();
                    System.out.println(value);
        
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
      System.out.println("This is my first program in java");
      System.out.println("Successful compilation");
    }//End of main
  }//End of FirstJavaProgram Classjav

Can I get some pointers on why this is happening? I am running with `java Dummy'. If this is vague let me know and I can elaborate further, I just cannot seem to understand why it says noClassDefFound despite JsonReader being a valid package within the gson-2.8.6.jar file as seen: https://javadoc.io/doc/com.google.code.gson/gson/latest/com.google.gson/com/google/gson/stream/JsonReader.html

  • I hope you run it with java -cp ... it's always recommended to do any project with external lib with Maven or Gradle, and use junit's test case to make sure your business logic works. – ShaharT Jul 06 '20 at 18:15
  • you should add the .jar to classpath even when running the compiled code, try `java -classpath "gson-2.8.6.jar:." Dummy` – MarcoLucidi Jul 06 '20 at 18:15
  • Thank you Marco! It worked, thank you very much for the help. – Eugene Tan Jul 06 '20 at 18:25

0 Answers0