DEV Community

DzifaHodey
DzifaHodey

Posted on

6 3

NoSuchFileException - How to avoid it when adding resources to a Spring Boot Application

Have you ever tried reading resources (json files, htm templates, text files etc) in your Spring Boot project, but got a java.nio.file.NoSuchFileException?

confused

The NoSuchFileException occurs if the file is not in the specified location. A similar exception that occurs is the FileNotFoundException.

1. Loading Files from a directory

A common approach developers use to add resources is to place the files in the src/main/resources/ directory, and then read the files from that path.
Although not the best practice, this approach works if the project is run locally. This is because the project directory is used as the current working directory during runtime.

For example, using the code below, I can read user data from a file and save the value in a string.

application.properties

filePath=src/main/resources/filename.txt
Enter fullscreen mode Exit fullscreen mode

ReadUserDataFromFile.java

public class ReadUserDataFromFile {
   @Value("${filePath}")
   private String dataFilePath;

   public String readDataFile() throws IOException {
      String data = new String(Files.readAllBytes(Paths.get(filePath)));
     return data;
    }
}
Enter fullscreen mode Exit fullscreen mode

The file is successfully read if it is in the src/main/resources/ directory.

Now, if the project is packaged as a JAR file, the NoSuchFileException will be thrown when trying to read the file. This happens because filename.txt is at the root folder of the JAR and it cannot be accessed using the file path.

2. Creating a Resource object

A better approach and a solution to the NoSuchFileException is to create a org.springframework.core.io.Resource object in your class and set the value to point to the file.
Example:

ReadUserDataFromFile.java

public class ReadUserDataFromFile {
   @Value("classpath:filename.txt")
   private Resource dataFile;

   public String readDataFile() throws IOException {
      String data = new String(dataFile.getInputStream().readAllBytes());
      return data;
    }
}
Enter fullscreen mode Exit fullscreen mode

With this approach, the file can be read both locally and from JAR file.

Heroku

Deliver your unique apps, your own way.

Heroku tackles the toil β€” patching and upgrading, 24/7 ops and security, build systems, failovers, and more. Stay focused on building great data-driven applications.

Learn More

Top comments (0)

Image of PulumiUP 2025

Let's talk about the current state of cloud and IaC, platform engineering, and security.

Dive into the stories and experiences of innovators and experts, from Startup Founders to Industry Leaders at PulumiUP 2025.

Register Now

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay