FileNotFound Exception in Eclipse

Discussion in 'Plugin Development' started by Altostratus, Apr 6, 2020.

Thread Status:
Not open for further replies.
  1. Hello,

    I am trying to refer to a custom CSV file in my plugin code, but I can't figure out the path I should be using in Eclipse. Everything I've tried results in a FileNotFound Exception. I seem to have tried everything.

    I've imported the archives.csv file into my src folder, and tried this:

    Code:
    try (BufferedReader br = new BufferedReader(new FileReader("src/archives.csv"))) { // }
    Then I tried without the "src" prefix. I tried moving the file into the project root folder, as well as into the package containing my Main class which has the above line.

    I then tried referring to the "physical" location of the file on my filesystem:

    Code:
    new FileReader("/Users/me/eclipse-workspace/myplugin/src/archives.csv")
    After reading several forum posts here and on StackExchange I ran the following:

    Code:
    System.out.print(System.getProperty("user.dir"));
    System.out.println(new File(".").getAbsolutePath());
    ...and discovered the paths were pointing to the folder containing the Minecraft server. I don't know what set this up (I didn't do it), but with this in mind I copied the file to the server and tried to point to it inside the server hierarchy itself, and not the Eclipse project, like so:

    Code:
    new FileReader("plugins/myplugin/archives.csv")
    This didn't work either.

    What am I doing wrong?

    Thanks.
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Altostratus There is a Plugin#getDataFolder().
    Then you can do this:
    Code:
    new File(Plugin#getDataFolder(), "archives.csv")
    Then you can check if it exists before feeding it to a reader.
     
  3. Thank you @timtower. It helps a little bit, but I still get an error.

    In Eclipse, I do this:

    Code:
    public class Main extends JavaPlugin {
    
        File f;
       
        @Override
        public void onEnable() {
            f = new File(getDataFolder(), "archives.csv");
            System.out.println(f.getAbsolutePath());
        }
    This outputs the full path to the file in the console: /Users/me/Desktop/mc/plugins/myplugin/archives.csv

    Later in my Main class, I have this:

    Code:
    try (BufferedReader br = new BufferedReader(new FileReader(f))) {    
    }
    However, this has *still* a red underline with "Unhandled FileNotFoundException" and when loaded in Minecraft still results in the same error.

    Code:
    Caused by: java.lang.Error: Unresolved compilation problems:
        Unhandled exception type IOException thrown by automatic close() invocation on br
        Unhandled exception type FileNotFoundException
    
        at com.myname.myplugin.Main.onCommand(Main.java:51) ~[?:?]
    
    Any ideas?
     
  4. Offline

    timtower Administrator Administrator Moderator

    @Altostratus f.exists() is a method that you can use to see if the file exists.
     
  5. OK, this is weird. To me at least.

    Code:
                getServer().getConsoleSender().sendMessage(Boolean.toString(f.exists()));
                try (BufferedReader br = new BufferedReader(new FileReader(f))) {
                 
                }
    This outputs "true" to the console if the "try" statement is commented out.

    If I uncomment the try statement I get the same error above and "true" isn't output.

    Please note that there are actually two errors, on the same line (which correspond to the trace output above):

    - "br" is underlined with "IOException thrown by automatic close() invocation on br"
    - "new FileReader(f)" is underlined with "FileNotFoundException"

    Can't figure this out.
     
  6. Offline

    timtower Administrator Administrator Moderator

    @Altostratus Turn it into a try catch block instead of a single statement
     
  7. Thank you and be blessed with your favorite enchantment.
     
Thread Status:
Not open for further replies.

Share This Page