0

I am trying to read the log file from the directory: src/main/resources/example.log The file is already there but it throws file not found exception. Here is my code:

public class Main {
    
    public static void main(String[] args) throws FileNotFoundException {

        //logfilepath = "src\\main\\resources\\example.log";
        Input input=new CommandLineInput();
        Textfilehandler textfilehandler = new Textfilehandler();
        Log logvariables=new Logvariables();
        Firstreadinglog firstreadinglog = new Firstreadinglog(textfilehandler,logvariables);
        Logfilehandler logfilehandler = null;
        Morereadinglog morereadinglog = new Morereadinglog(textfilehandler,logvariables);

        LogAnalyzerApp app=new LogAnalyzerApp(textfilehandler,firstreadinglog,morereadinglog,input);
        app.show();
    }
}

And the error message is:

java.io.FileNotFoundException: /example.log (No such file or directory) Exception in thread "main" java.lang.IndexOutOfBoundsException: Index -1 out of bounds for length 0 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248) at java.base/java.util.Objects.checkIndex(Objects.java:373) at java.base/java.util.ArrayList.get(ArrayList.java:427) at kln.se.ass2.logfile.Morereadinglog.getcurrentlogstates(Morereadinglog.java:50) at kln.se.ass2.LogAnalyzerApp.show(LogAnalyzerApp.java:35) at kln.se.ass2.Main.main(Main.java:29)

I entered the file path as follows: src\main\resources\example.log

Mr. Discuss
  • 355
  • 1
  • 4
  • 13

2 Answers2

0

You can try to use the full path of the file using resource of Maven that automatically sets the current working directory, then you can just use:

File resourcesDirectory = new File("src/main/resources/example.log");
    
String logfilepath = resourcesDirectory.getAbsolutePath();
0

Problem is that I use Linux OS. I entered the directory with backslashes as in Windows. Now I figured it out. Thank you.