ClassResource image not displaying in jar, why??


Well, this is a post with a question, and the solution! But, it seems like there is a discrepancy somewhere and not sure if its just my understanding that needs to be corrected, or something code related.

I have an icon in an add on that I’ve made, and I’m referring to it via a ClassResource like this:

ClassResource classResource = new ClassResource(resourcePath, application);

where resource path =


which works fine when I run the application locally. However, it does NOT work when I package as a jar, and I found that I was forced to refer to the icon like this:


Note the leading slash.

Why is that? I like things to work regardless of the environment (locally running it in or as a jar in another project) so I’m not sure if this means there is something in vaadin that should be changed, or this is just the way things are? Can someone please clarify?



Are you sure that the add.png file is included in your jar file ?


Yes, I don’t change anything between builds of the jar other than the leading slash - the icon wouldn’t show up in either case if it was missing…

And, I manually verified as well.

I guess this has something to do with how Class.getResourceAsStream works in different environments.,5.0/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)

This is what the ClassRessource uses internally to find the data.

Seems like it, thanks!