简体   繁体   中英

Java: File relative path - No File Found

I coding a java send mail program which attach a test report file at relative path.

Questions: How to setup the path so that the file can be found?

Test Report Directory:

com.securister\report\Test-Report.html

Java File:

com.securister\src\test\java\com\securister\TestManager

I tried setup like

String filename = "\..\..\..\..\..\..\report\Test-Report.html"

but end up no file found.

Should i using classpath?

You have the structure of src/main/resources and src/test/resources , put the

target file under the resource in a test as I guess

, And read the file from the path src/test/resources/report/Test-Report.html

You can print the current directory of your java program where it is executed from with this java code,

System.out.println("CurrentDir: " + (new File(".").getCanonicalPath()));

Say it prints,

CurrentDir: D:\XXX\workspace\test Then you can correctly choose a path through which your file can be correctly located.

Most likely, your src folder should be in the test directory and in that case you can either move your file from src folder to test folder or refer your file in your code like this,

..\Test-Report.html

which should be able to read your file.

Let me know if this works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM