1

I am stuck at two parts really. A) I need the program to find the directory of the RUNNING JAR FILE and check if there is a file called "credits.txt" in the same directory. B) If not, it would create the file in the SAME DIRECTORY.

The main issue is not being able to get the path of my file.

Say the running jar file was in a folder called "Server", the program would save the name "Server" in a string and then check if a file exists in that string. If so, do nothing, otherwise create the file.

@Override
public void onEnable(){
    getLogger().info(ChatColor.GREEN + "Credits has been enabled!");
    File file = new File("Credits.txt");

    //HERE I NEED THE PROGRAM TO CHECK WHAT DIRECTORY THE RUNNING JAR FILE IS FROM
    if (file.exists(//IN THE SAME DIRECTORY AS THE RUNNING JAR FILE)){
        getLogger().info(ChatColor.DARK_GREEN + "Credits File Exists");

    }else{
        getLogger().info(ChatColor.DARK_RED + "Credits File Doesn't Exist!");
        //HERE IT NEEDS TO CREATE THE FILE IN THE SAME DIRECTORY OF THE JAR FILE "credits.txt"

    }
A.Tol
  • 33
  • 1
  • 8
  • 4
    And what is your difficulty? Note that the current working directory (where it looks for files without a full path) is not necessarily where the `jar` file is. – RealSkeptic Mar 19 '16 at 13:25
  • 1
    No, it is not a "simple thing to do", and in fact Java seems to try to discourage you from doing this. I don't remember the details of how to find the Jar directory, but it does involve some contortions. I seem to remember the need to use some "security" type classes or methods. Search on finding the path to a jar file to get the details. – Hovercraft Full Of Eels Mar 19 '16 at 13:59
  • 1
    Ah, [here it is](http://stackoverflow.com/questions/2837263/how-do-i-get-the-directory-that-the-currently-executing-jar-file-is-in). Not "security" but "protected". `getClass().getProtectionDomain().getCodeSource().getLocation()` – Hovercraft Full Of Eels Mar 19 '16 at 13:59
  • 2
    But having said this, this might not but what you want to do, and your question may in fact be an [XY Problem](http://mywiki.wooledge.org/XyProblem) where you ask "how do I fix this code problem" when the best solution is to use a different approach entirely. Consider telling us the overall problem that you're trying to solve rather than how you're currently trying to solve it. – Hovercraft Full Of Eels Mar 19 '16 at 14:01
  • @HovercraftFullOfEels I'm sorry if I didn't explain the part I'm stuck at properly. I have updated the first post – A.Tol Mar 19 '16 at 16:34

1 Answers1

1

If you're making a Bukkit plugin, you can use getDataFolder() in your main class. Then you can check if it exists and then create it if it doesn't.

public void onEnable(){
    getLogger().info(ChatColor.GREEN + "Credits has been enabled!");

    File pluginDirectory = getDataFolder(); //getting the data folder
    if(!pluginDirectory.exists()){
        pluginDirectory.mkdir(); //Creating the plugin data folder if it doesn't exist.
    }

    File file = new File(pluginDirectory+File.seperator+"Credits.txt"); //Credits.txt inside the plugin directory

    if (file.exists()){ //Checking if Credits.txt exists
        getLogger().info(ChatColor.DARK_GREEN + "Credits File Exists");

    }else{
        getLogger().info(ChatColor.DARK_RED + "Credits File Doesn't Exist!");

        file.createNewFile(); //You probably need to create it too
    }
}

If you want to create it for storing settings, consider using getConfig() instead of creating a file yourself. It's much easier.

marti201
  • 376
  • 2
  • 13
  • Thanks! I might comment again in abit once I've figured out what exactly is going on but I think I get it now :) – A.Tol Mar 19 '16 at 16:57
  • Thanks, right now a folder is being created upon server launches and reloads which is great, but this folder is always overwritten upon server launches, it creates a new folder every time – A.Tol Mar 19 '16 at 17:18
  • Basically, this line: if(!pluginDirectory.exists()) is always true, so a new folder seems to be created every time... And the console prints "Credits file Doesn't Exist" – A.Tol Mar 19 '16 at 17:19