2 images one ist showing other not! Please Help

Hello i´m new to vaadin. I need some help.

i have 2 images in Webcontent/images

let´s say logo1.jpg and logo2.jpg

logo2 is the scaled version of logo1.

logo1 is loading an showing.
logo2 not!

I use this code:
String logo_path=context.getHttpSession()
+ “/logo2.jpg”;

	header.addComponent(new Embedded("", new ClassResource(


If I start the application on the Server i see the red x icon that image is not found.
If print the path to the logo2 i get something like this:


The image exits in that path, so why doesn´t it load?

Do you have any Idea what i did wrong?


I have no idea why that would work for even one image. You can use ClassResource to get images from the class path, typically images that you have beside your source code. You can’t use ClassResource to get images from outside the class path (WebContent/WEB-INF/classes).

If you have put images under WebContent/images, you should use ExternalResource. You need to include the context root in the path.

ExternalResource resource = new ExternalResource("/book-examples/images/image.png");
Embedded image = new Embedded("External Image", resource);

See the
on-line example

Hello Marko, thanks for your advice.

The behavier got even stranger, i just deleted all images from the images Folder but logo1.jpg is still showing even after refreshing the cache with strg + f5

I was searching for the image in the deploy folder but didn´t find any, so i expected that nothing is loaded.
Maybe i have a problem with the server configuration?

I´ll try your example. I really hope that works.

This doesn´t work ether

		ExternalResource resource = new ExternalResource("/kwfms_v0001/images/kremer_logo.jpg");
		Embedded logo = new Embedded("", resource);

I think in this case i have to do some mapping in web.xml of the Application here is my web.xml as it is right now:

I changed the mapping als followed:

  	<servlet-name>Kwfms_v0001 Application</servlet-name>

now i get a 404 when i start my application :frowning:

I found a soulution:

// Images Folder is located here /WebContent/images

// i get the absolute path of the images folder and add the name of the image
		String path=context.getHttpSession().getServletContext().getRealPath("/images")+ "/mylogo1.jpg";
// i create a simple FileResource
		FileResource resource = new FileResource(new File(path),this);
		Embedded logo = new Embedded("mylogo", resource);

works fine until now
now my Question:
Can i expect any problems from my solution? Or why does nobody recommended to do like this?

Sure, using a FileResource is just as fine. That way you don’t have to open up the servlet URL for the image directory with a /foo/* for the url-pattern.

Using getApplication().getContext().getBaseDirectory().getAbsolutePath() to get the base directory of the application content may sometimes be a bit easier.

sorry for the silly question mate but how did you get the “context” object?

actually I am trying to display an image in my portlet and I am using following code inside the init() method:

ClassResource resource = new ClassResource("image_with_text_477x120.jpg", getMainWindow().getApplication());
Embedded image = new Embedded("Yes, logo:",resource);

In the portlet, image is not displayed but i can see the text “Yes, logo:”… any idea how to do this? image is in my portletRoot/WEB-INF/classes directory. I am using Liferay and Tomcat…

It should work somewhat like that. If the image is in the classes root, you probably need to give the root slash for the filename.

this example

Regarding your other question, you can get the context from the application object with getContext(). The ApplicationContext interface doesn’t have the getHttpSession() method that Ingo used, but you have to cast it to WebApplicationContext. This only works in a regular server; the context class different under a portal or GAE.

Thanks for the help Marko. :slight_smile:

Same code works now if i place it outside init() method. I created a Form, added couple of Panels to the Form. In first panel i used the same code (above one) and in the 2nd Panel i created some form fields. This Form is then added to the main window in the init() method and the image gets loaded perfectly. But for some reason, image does not gets displayed if added directly to the main window in init() method. This solution works for me though as the image(which is the logo) is being loaded as part of the initial UI.

Hello Sulman, you the context in this way

context = (WebApplicationContext) getContext();

Here the complete example:

import com.vaadin.terminal.gwt.server.WebApplicationContext;
import com.vaadin.terminal.FileResource;
import java.io.File;

context = (WebApplicationContext) getContext();
String path=context.getHttpSession().getServletContext().getRealPath("/images")+ "/mylogo1.jpg"; 
FileResource resource = new FileResource(new File(path),this);       
Embedded logo = new Embedded("mylogo", resource);