0

I have a class within a Maven project that I am trying to use to get user data and map it to a json file located in another folder outside of the one the compiled jar is located.

My question isn't necessarily how to append data to a json file, but rather how I can get the location of the json file I'd like to append my data too.

Take for instance I have a project with folders like:

Project/src/main/java/com.website.project/Class.java

Once that I have this project packaged into a jar file, I would then place it in a folder where it would be run:

App/jars/Project.jar

I want it to access a json within the folder:

App/json/file.json

What code would I need to write to access the directory from my Class.java? I am sorry if this was confusing, I'm not the best when it comes to Stack Overflow, but thank you so much for any help in advance!

2 Answers2

0

First approach:

Please test if you can find the file with:

File file = new File("./../json/file.json");
System.out.println("File exists: " + file.exists());

Relative paths in java start with ./. When you export your jar the relative start path is the location of the jar => App/jars/ so you need to go one folder up with ../ and after that go inside /json/file.json

The disadvantage of this approach:

To work with file outside your java project (assuming your java project is in java directory) means that you have to create every time this folder structure everywhere. For example in your case if you want to work also in you IDE inside of your workspace of projects you have to add directory json and after that your file.json.


Second approach:

Another solution can be to add your file inside the java project itself. Then you will be able to read your file easy with getClass().getResourceAsStream("file.json");. Consider that file.json is inside your class's package. This way you can test and see 'file.json' also inside your IDE.

The disadvantage of this approach:

Pay attention that if you use your file inside the Java project it will end up in the jar. When this happen you no longer can access it as File. That why I am useing getResourceAsStream method in this case. To read more about this see answer: https://stackoverflow.com/a/20389418/6068297

Also you have to know that getClass().getResourceAsStream("file.json"); will not work in static methods for example in public static main(String[] arg).

Update:

Also the jar file is meant to be archive so it must stay unchanged. So you can not (you should not) write back changes to file inside the jar. If you need to have some modifiable file then you should create it outside the jar. You can add it relative to the jar location or in a place like home directory of the current user where relevant files to the current user (who is using your application) can be created without some additional permissions.


Third approach:

You mention that you are using Maven project. There is a folder in the Maven project which is called resources => src/main/resources. This folder end up in the classpath so you can also put your file file.json there and read it as second approach with getResourceAsStream. This way you can have clear separation between java classes and other files.

The disadvantage of this approach:

The same disadvantages as in the second approach.


I hope it helps.

Level_Up
  • 789
  • 5
  • 19
  • 1
    Thank you for all the suggestions! I'm very new to Maven projects so this is a great resource to me. One question I have however is with the third approach, can I then access the json after my jar is compiled and is making changes to that json? – Rogelio S. May 18 '20 at 22:35
  • Yes you can read the json file as resource stream but you can not modify it. Remember that you can not get File instance inside the jar. If you need to write some information to the file I will suggest you to use the first approach with the file structure. – Level_Up May 19 '20 at 11:31
0

You could keep your App/json/file.json in src/main/resources folder.
Path - src/main/resources/App/json/file.json

Then you could access it by :

JSONTokener parser = new JSONTokener(this.getClass().getResourceAsStream("/App/json/file.json"));
      JSONObject jobj = new JSONObject(parser);
      jobj.put("style", style[i]); // If you want to add a new key value or just replace it

The file itself will get packaged in JAR file Don't forget to include org.json in your pom.xml dependency.

Import this in your class:

import org.json.JSONObject;
import org.json.JSONTokener;
ProGamer
  • 420
  • 5
  • 16
  • Why would the path be src/main/resources/App/json/file.json and not src/main/resources/file.json? – Rogelio S. May 18 '20 at 22:36
  • It could be src/main/resources/file.json too. I just took the path mentioned in the question as an example. Hope it helps :) – ProGamer May 19 '20 at 04:08
  • Just remember this JSON will be included in the JAR. And you probably cannot make changes to the JAR. – ProGamer May 19 '20 at 04:10