Java中使用Maven时的文件未找到异常

huangapple go评论58阅读模式
英文:

file not found exception in java with maven

问题

我正在尝试从目录中读取日志文件:src/main/resources/example.log
文件已经存在,但是会抛出文件未找到的异常。
以下是我的代码:

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();
    }
}

错误信息如下:

java.io.FileNotFoundException: /example.log (文件或目录不存在) 异常出现在线程 "main" 内
java.lang.IndexOutOfBoundsException: 索引 -1 超出长度范围 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)

我按照以下方式输入了文件路径:
src\main\resources\example.log

英文:

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

答案1

得分: 0

你可以尝试使用Maven的资源(resource)功能,通过使用文件的完整路径,它会自动设置当前工作目录,然后你可以这样使用:

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

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();

答案2

得分: 0

问题是我使用Linux操作系统。我像在Windows中一样输入带有反斜杠的目录。现在我明白了。谢谢。

英文:

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

huangapple
  • 本文由 发表于 2020年9月13日 22:47:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63872086.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定