2

I'm making a maven project and I'm trying to use Gson or simpleJson as a parser for my project.

I have a file called moves.json in my resource folder and when I'm trying to run this code:

 JSONParser pr = new JSONParser();
Object el = pr.parse(new InputStreamReader(new FileInputStream("moves.json")));

I get a

Exception in thread "main" java.io.FileNotFoundException: moves.json (No such file or directory)

and when I'm trying this code:

 JsonReader reader = new JsonReader(new InputStreamReader(test.class.getResourceAsStream("moves.json")));
    JsonElement movelist =  new JsonParser().parse(reader);

I get a:

Exception in thread "main" java.lang.NullPointerException

I tried using ClassLoader and getResource and other method I searched on the web but I get the same error of null pointer exception

this is the full class:

import com.google.gson.*;
import com.google.gson.stream.JsonReader;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.*;


public class test {

public static void main(String [] args) throws IOException, ParseException {
    JSONParser pr = new JSONParser();
    Object el = pr.parse(new InputStreamReader(new FileInputStream("moves.json")));

    JsonReader reader = new JsonReader(new InputStreamReader(test.class.getResourceAsStream("moves.json")));
    JsonElement movelist =  new JsonParser().parse(reader);
    System.out.println("");
}

}

eyal mazuz
  • 109
  • 1
  • 2
  • 10
  • This appears to be the same as all other similar questions, and the solution is to change your path to the file to where Java is looking. Have you checked out the similar questions on this site, as you'll find many. – DontKnowMuchBut Getting Better Aug 25 '18 at 12:16
  • Please check out [some of these questions](https://www.google.com/search?q=java+cannot+find+file+or+resource+site:stackoverflow.com) – DontKnowMuchBut Getting Better Aug 25 '18 at 12:17
  • Also check out [this question's answer](https://stackoverflow.com/questions/27010487/cant-access-resources-in-executable-jar) for more on finding resources – DontKnowMuchBut Getting Better Aug 25 '18 at 12:20
  • yes I checked similar questions non of them helped me for some reason, I managed to find an answer, thank you – eyal mazuz Aug 25 '18 at 12:25
  • As others said, the path Java is looking at is not the path you expect it to look at. Thus, your path is wrong and Java does not find the file. Inspect the path by printing it. For example `System.out.println(Paths.get().toAbsolutePath());`. This is the start of the relative path java looks at when you write `new FileInputStream("moves.json");`. It's probably **not** where you expected it to be. Adjust the path accordingly and it will work. – Zabuzard Aug 25 '18 at 12:27
  • Always use a slash to specify path relative to classes folder. See accepted answer of the question I closed this as a duplicate of for an example – Nathan Hughes Aug 25 '18 at 12:29

1 Answers1

0

As the resource is on your class path, you should use class loading mechanism to open a stream:

Object el = pr.parse(new InputStreamReader(
                   test.class.getResourceAsStream("moves.json")));
ernest_k
  • 44,416
  • 5
  • 53
  • 99