5

In the example given here:

https://vaadin.com/docs/framework/application/application-resources.html

the images-folder is put inside the WEB-INF directory of the application.

In my Vaadin Spring application, I do not have a WEB-INF directory, so I put the images folder inside the "resources" folder instead. Here is what the folder structure inside of "src/main/resources" and inside of "target" looks like:

enter image description here

The problem is that my application cannot access the images there. I always get the same "File not found" exception.

I tried out different path descriptions, among others, the following:

VaadinService.getCurrent().getBaseDirectory().getAbsolutePath() + "images/pic.png"

VaadinService.getCurrent().getBaseDirectory().getAbsolutePath() + "/classes/images/pic.png"

VaadinService.getCurrent().getBaseDirectory().getAbsolutePath() + "/target/classes/images/pic.png"

VaadinService.getCurrent().getBaseDirectory().getAbsolutePath() + "/applicationName/target/classes/images/pic.png"

... nothing worked !!

How can I make this images accessible to my Vaadin Spring application?

KeatsPeeks
  • 19,126
  • 5
  • 52
  • 83
steady_progress
  • 3,311
  • 10
  • 31
  • 62
  • 1
    Your code suggests that you're trying to load the image as a *File Resource*. However, if the file is inside the project and on the classpath, don't you want to use a *Class Loader Resource* instead? Perhaps you can update the question with the code you're using to create the Image component? – Mike Patrick Oct 26 '17 at 02:41

1 Answers1

7

The example you linked to uses FileResource. It won't work with jar-packaged resources.

Preferably, you can put your images in src/main/resources/VAADIN/themes/{theme}/ and use a ThemeResource :

// Image as a file resource
FileResource resource = new ThemeResource("images/image.png");

Alternatively, put your resource to src/main/resources/ and then access it from classpath:

getClass().getResourceAsStream("/images/image.png")
KeatsPeeks
  • 19,126
  • 5
  • 52
  • 83
  • thank you very much for your answer, it solved my problem ... in case you have time, please have a look at another question of mine related to this one (actually I thought the answer to this post here would also solve the other problem, but this was not the case so I now created a second post for this other problem): https://stackoverflow.com/questions/46960392/how-to-create-file-on-server-with-file-path-relative-to-application – steady_progress Oct 26 '17 at 17:29