1

I am using eclipse for reading a jsonfile. I put the jsonfile in my src->main->java->testjson->jsonfile.json . Now I am trying to read the jsonfile. But my progam cannot find the file. I get the output "nothing". Here is the code I already implement:

JsonParser parser = new JSONParser();
try{

Object obj = parser.parse(new FileReader("jsonfile.json"));
JSONObject jsonObject = (JSONObject) obj;
String name = (String) jsonObject.get("name");

}

catch(Exception e){
System.out.println("nothing");
}
5
  • 2
    Print out the stack trace of the exception instead of printing a useless "nothing" string in the catch block. e.printStackTrace(); Commented May 10, 2019 at 10:45
  • use the spring library for parsing the Json Commented May 10, 2019 at 10:46
  • @Jesper , ur right. I get the message cannot find file. But the file is in the project Commented May 10, 2019 at 10:47
  • @LovaChittumuri where should I use it? in the maven respositories? Commented May 10, 2019 at 10:48
  • path to your jsonfile.json is invalid Commented May 10, 2019 at 10:49

4 Answers 4

3

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());
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this helped a lot, with good explanation and good code snippet
1

As you're saying that you use Eclipse, i assume you also run your code via Eclipse. As a default, the working directory when executing a Java program in Eclipse is the root folder of the project. Therefore, I suggest to put your jsonfile.json in the root folder of your project instead of src/main/....

Furthermore, you should not catch Exception. Catch more specific like IOExceptionor JSONException and then print the exception message (e.getMessage()), then it is much easier to solve the problem.

2 Comments

@ but therefore I use my C:/home.. later I want to use the json on a testserver.. How should I now implement that? When I put the full path of my home folder it will not work on the server. how can I do that? thx
FileReader will look for the file relative to where you started your java program (actually the working directory of the process which executes the jar). So if you package your program as an executable jar and execute that on your server, the json file should be parallel to the jar file in case you still use new FileReader("jsonfile.json"). However, for a more stable setup and deployment, i would recommend the approach from the other answer and bundle the json file within your jar file and load it as a resource.
0

The file "countries.geo.json" is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.

You have to use the data folder as a source.

if Images not appear. Click on this links to see the images

Right-Click on the data folder

You then see Buid path option

Click on "Use as a source folder"

2 Comments

please try to give some deatils in answer (small explanation) if you are adding links of any images or any URL
I am new to answering query on stackoverflow. I had tried to add detail like basic steps like ->Right-Click on the data folder -> You then see Buid path option ->Click on "Use as a source folder"
-1

Give the full path to the JSON file instead of the file name.

If the file path is home/src/main/java/testjson/jsonfile.json

String path = "home/src/main/java/testjson/jsonfile.json";
Object obj = parser.parse(new FileReader(path));

2 Comments

Later I will execute this on a nother test server. Therefore I need the path in the project right? I used ("/testjson/jsonfile.json") but its still not working :( "cannot find the file")
Can you try with the entire path rather than the relative path?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.