2

I have created a Vaadin application that tries to use the ClassResource vaadin class to load some icons along with some vaadin components but I cannot see the icons in the final application. In icons place I see a blue question mark. The application is deployed on tomcat (latest version). Here is part of the code.

IconsUI.java :

tf.setIcon(new ClassResource("email.png")); cb.setIcon(new ClassResource("note.png")); ta.setIcon(new ClassResource("document.png")); bt.setIcon(new ClassResource("ok.png")); 

These image files are located in the same package as the IconsUI.java class.

My web.xml is the following :

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>icons</display-name> <context-param> <description> Vaadin production mode</description> <param-name>productionMode</param-name> <param-value>false</param-value> </context-param> <servlet> <servlet-name>Icons Application</servlet-name> <servlet-class>com.vaadin.server.VaadinServlet</servlet-class> <init-param> <description> Vaadin UI class to use</description> <param-name>UI</param-name> <param-value>MavenVaadinIcons.IconsUI</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Icons Application</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app> 
6
  • where are your icons placed? Commented Dec 30, 2013 at 9:18
  • They are meant to beautify the components but the problem is that they are not get loaded by tomcat. Commented Dec 30, 2013 at 12:07
  • where have you placed them in your project (path)? Commented Dec 30, 2013 at 12:24
  • In the same package with the java class that is trying to use them(IconsUI.java) Commented Dec 30, 2013 at 13:16
  • Do you use Maven? If so then the icons must be placed into the src/main/resources/ folder. Commented Dec 30, 2013 at 13:23

2 Answers 2

4

To load an image using ClassResource, your images should be available in the classpath of your final web application. You can add your images to classpath by placing the images in src/main/resources folder of your maven source.

Also, you need to add a leading / character to image file names. The ClassResource constructor you used doesn't have a Class argument, so it uses the current UI class as the class. The resource you specified is looked up in the same package as the UI class. By adding a leading / character, the file will be looked up in the classpath root.

tf.setIcon(new ClassResource("/email.png")); cb.setIcon(new ClassResource("/note.png")); ta.setIcon(new ClassResource("/document.png")); bt.setIcon(new ClassResource("/ok.png")); 
Sign up to request clarification or add additional context in comments.

Comments

1

Because you use Maven, you must place resource files into the src/main/resources folder. If you don't have that folder, you can create it.

If your image's path is for example src/main/java/com/example/email.png put it into a corresponding package in resources: src/main/resources/com/example/email.png.

8 Comments

I've already tried to put them in the main/resources folder that I created manually but I still cannot see them loading.
Do you believe that I should change the ClassResource class argument?
Did you put those into the correct package in resources folder? However, I would consider to place the images into theme and use ThemeResource.
I have already seen the theme solution, but why can't I use ClassResource class as the author of the book I read does?(Vaadin 7 UI Design By Example).Basically I have not been able to access any files at all in other projects of this book too. Does tomcat is so problematic when it comes to vaadin projects?
ClassResources work when you have images in correct place in class path.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.