0

I have a Java Spring project with a file in some package (In project). But when I want to open it,

Tomcat handle it and searching this file in C:\Tools\apache-tomcat-9.0.35\bin

I am getting below exception while getting file by new File("./src\\main\\resources\\directory\\myFile.txt");

Exception

java.io.FileNotFoundException: C:\Tools\apache-tomcat-9.0.35\bin\src\main\resources\directory\myFile.txt
Musaddique S
  • 1,539
  • 2
  • 15
  • 36
  • Please, try to format your code blocks properly. Follow this link for further explanation: https://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks – Chema Jun 11 '20 at 20:01

1 Answers1

0

The issue is the relative file location you are using "./src\main\resources\directory\myFile.txt".

When your code is running on tomcat your current directory is "C:\Tools\apache-tomcat-9.0.35\bin" and thats why the absolute path is "C:\Tools\apache-tomcat-9.0.35\bin\src\main\resources\directory\myFile.txt"

Possible solutions:

  1. Include the file as a resource and use a classpath loader to load it. See example below.

  2. Use an absolute path to your file. (not a good solution)

Example from this link Read file from resources folder in Spring Boot

File file = new File(getClass().getResource("directory/myFile.txt").getFile());
Mike Murphy
  • 1,006
  • 8
  • 16