I am rather new to VSCode and maven and am trying to work with a JSON file in java, i found out about json simple and decided to try it but i just cant get it to work. The goal is to convert the json file (which contains a json array) into an object i can work with in java.
When i run the following code:
package pandemic;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class Game {
public static void main(String[] args) throws FileNotFoundException, IOException, ParseException {
JSONParser parser = new JSONParser();
FileReader reader = new FileReader(".\\jsonfiles\\cities.json");
Object obj = parser.parse(reader);
JSONArray citiesInfo = (JSONArray) obj;
System.out.println(citiesInfo.get(1));
}
}
I get this message:
Error: Unable to initialize main class pandemic.Game
Caused by: java.lang.NoClassDefFoundError: org/json/simple/parser/ParseException
I tried to use gson as well but got the same error. I even went as far as to do a clean install of a new jdk to make sure that isnt the issue. No luck. I checked my pom.xml many times and its the same as any example i have seen. Does anyone here know what i can do to fix this?
Edit: Have also tried updating the project and cleaning the java language and resetting the workspace.
CodePudding user response:
If your goal is to get it working, Why don't you ignore the ParseExeption file altogether ? Just try running the code with Generic Exceptions ? You can focus on the ParseException when you get some experience working with the code.
Remove import org.json.simple.parser.ParseException;
and change public static void main(String[] args) throws FileNotFoundException, IOException, ParseException {
to public static void main(String[] args) throws FileNotFoundException, IOException, Exception {
CodePudding user response:
When your project lacks needed dependency, java extension compiles .java
file, then the error occurs. Add the following code in pom.xml
, the extension Maven for java will rebuild project since it detects changes in pom.xml
. After that, you may run .java
file successfully.
<dependency>
<groupId>org.apache.clerezza.ext</groupId>
<artifactId>org.json.simple</artifactId>
<version>0.4</version>
</dependency>