Home > Software engineering >  Oracle Java Json not working even after adding classpath
Oracle Java Json not working even after adding classpath

Time:07-07

I am using openjdk version 11.0.15 on Debian 4.19. I want to use the JSON library available in Java: javax.json-1.0.jar. After downloading the jar file, I have added the classpath like so:

export CLASSPATH=/Diss/frontend/javax.json-1.0.jar

echo $CLASSPATH:
/Diss/frontend/javax.json-1.0.jar

my code is this:

import javax.json.Json;
import javax.json.JsonReader;
import javax.json.JsonStructure;
import javax.json.JsonObject;

public class ns3_inputs {
        public static void main (String[] args)
        {
                JSONObject obj = new Json.createObjectBuilder();
                obj.add("foo", "bar");
                obj.build();
        }
}

I compile like this: javac -cp .:$CLASSPATH ns3_inputs.java

to which I get the following error:

error: cannot find symbol
        JSONObject obj = new Json.createObjectBuilder();
        ^
  symbol:   class JSONObject
  location: class ns3_inputs
ns3_inputs.java:25: error: cannot find symbol
        JSONObject obj = new Json.createObjectBuilder();
                                 ^
  symbol:   class createObjectBuilder
  location: class Json
2 errors

The java source code file and the jar file are in the same directory. Am I supposed to place the jar file in a specific path? I am very new to Java so I may be missing something very basic here. I am trying to build a desktop Java application and I cannot use any libraries suitable for android. Please suggest a solution

CodePudding user response:

The class name is JsonObject, not JSONObject.

The following compilation is successful.

~ $ export CLASSPATH=~/Downloads/javax.json-api-1.0.jar 
~ $ 
~ $ cat ns3_inputs.java 
import javax.json.Json;
import javax.json.JsonObjectBuilder;

public class ns3_inputs {
        public static void main (String[] args)
        {
                JsonObjectBuilder obj = javax.json.Json.createObjectBuilder();
                obj.add("foo", "bar");
                obj.build();
        }
}
~ $ 
~ $ javac ns3_inputs.java 
  • Related