Search code examples
javajsonbukkit

getting json file in bukkit


I'm trying to get my JSON in my plugin, but it doesn't work. Here is my code to get it and here is the path: static File json = new File("config.JSON");. I tried to use the GetDataFoder() method but I can't because I'm using static methods.

public class JSONReader extends JavaPlugin {
    static File json = new File("config.JSON");

    static File getJSON() {
        return json;
    }

    static JSONObject setupJson() {
        try {
            JSONParser jsonParser = new JSONParser();
            Object parsed = jsonParser.parse(new FileReader(json.getPath()));
            JSONObject jsonObject = (JSONObject) parsed;
            return jsonObject;
        } catch (ParseException | IOException e) {
            return null;
        }
    }
}

Solution

  • All paths are evaluated relative to your working directory, which is the one containing your server executable. Bukkit provides convenience methods for file access through JavaPlugin:

    • Referencing files within the plugin's data folder

      File f = new File(getDataFolder(), "config.json");
      
    • Retrieving files within the plugin's jar

      InputStream is = getResource("config.json");