Your file within the project is called a "resource", which will be bundled in the resulting jar-file.
In maven projects such files resides in a special folder resources (like src/main/resources/testjson/jsonfile.json), in many other project types, these files are located directly beneath the java files.
Therefore you cannot read it with FileReader, because it will not be a regular file, but zipped inside the jar file.
All you have to do is to read the file with this.getClass().getResourceAsStream("/testjson/jsonfile.json").
Your parser should be capable to read from an InputStream instead of a Reader.
If not, utilize an InputStreamReader with the correct encoding (JSON files should be UTF-8, but that depends...)
Code:
try (InputStream is = this.getClass().getResourceAsStream("/testjson/jsonfile.json"); ) {
Object obj = parser.parse(is);
} catch (Exception ex) {
System.out.println("failed to read: "+ex.getMessage());
}
Code if parser does not support InputStream:
try (InputStream is = this.getClass().getResourceAsStream("/testjson/jsonfile.json");
Reader rd = new InputStreamReader(is, "UTF-8"); ) {
Object obj = parser.parse(rd);
} catch (Exception ex) {
System.out.println("failed to read: "+ex.getMessage());
}
e.printStackTrace();jsonfile.jsonis invalid