0

I'm setting up logic to write string to an html file

I tried reading details.html file placed in \src\main\resources\static\

File htmlTemplateFile = new File("details.html");

OR

File htmlTemplateFile = new File("\src\main\resources\static\details.html");

When I run this code, I am getting file not found exception , although the file is placed in \src\main\resources\static\

I have tried both above mentioned alternatives, still file not found exception is being thrown.

ERROR : msgjava.io.FileNotFoundException: File 'details.html' does not exist

Shawn Saldanha
  • 63
  • 1
  • 1
  • 8
  • You need either an absolute path (from root) or a relative path (from the execution context), neither of which you have. If you want to open it as a *resource* then you should do that, and the path would be relative to the classpath. – Dave Newton Apr 03 '19 at 13:18
  • I'm exploring java as fresh, so sorry for the doubt But, Where can I find my relative path? @Dave Newton – Shawn Saldanha Apr 03 '19 at 13:54
  • Try with `new File("static/details.html");` Everything under `/resources` is considered as classpath entries. – leopal Apr 04 '19 at 08:04

1 Answers1

0

Spring Boot will automatically add static web resources located within any of the following directories:

/META-INF/resources/
/resources/
/static/
/public/

From How can I serve static html from spring boot?

So the following works (tested):

@SpringBootApplication
public class StackoverflowApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(StackoverflowApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {


        // index.html under /src/main/resources/static
        File file = new File("index.html");

        System.out.println(file.getName());

    }

}
rieckpil
  • 10,470
  • 3
  • 32
  • 56