how to get the path to the WEB-INF directory

hello everyone,

I placed a custom config file in the WEB-INF directory of my vaadin web app…

How can I get the path to the WEB-INF directory (and of course the config file) at runtime

Thanks in advance


Hi Peter,

you can construct the path to a file inside the webapp folder by using WebApplicationContext, which you can get by calling getContext of your Vaadin application class instance:

WebApplicationContext context = (WebApplicationContext)getContext();
File webinfFolder = new File ( context.getHttpSession().getServletContext().getRealPath("/WEB-INF") );

Alternatively, you could want to read your config file at app startup - in this case just register your ServletContextListener and in contextInitialized(…) callback method you’ll find the ServletContext as a incoming parameter

thanks for your answer

I find it very usefull

Halo Dmitri Livotov,

I am newbie for vaadin.

would you explain me more about WebApplicationContext, I have got error when wrote this line

“com.vaadin.terminal.gwt.server.WebApplicationContext cont = (com.vaadin.terminal.gwt.server.WebApplicationContext cont) getContext();”

The error is " the method getContext() is underfined


It’s a method in the Application class which you extend in your application.

Hi Salman,

as Marko mentioned, please make sure you’re calling getContext from your application class which extends Vaadin’s Application.

@Marko Grönroos and Dmitri Livotov,

my class extends with panel’s class, that why I got error and it’s running as well when I changed my class extended with Application class.

Thank for your help.


[/size]Could you please explain where/how to call the getContext method?
This is my main Vaadin application class where I call getContext as you explained - but I get the error:

The method getContext() is undefined for the type MyprojectUI

public class MyprojectUI extends UI {
protected void init(VaadinRequest request) {

WebApplicationContext context = (WebApplicationContext)getContext();
File webinfFolder = new File (context.getHttpSession()
.getServletContext().getRealPath(“/WEB-INF”) );

    final VerticalLayout layout = new VerticalLayout();
    Layout layout_component = new Layout();

What am I doing wrong?


[/size] Also, I have tried the following to retrieve the WEB-INF path:

String basepath = VaadinService.getCurrent().getBaseDirectory().getAbsolutePath(); System.out.println(basepath); However, this gives me the path:


Instead of:


So why is that?


[/size] third question…

File pfad = new File(""); System.out.println(pfad.getAbsolutePath()); Doing that will print out:

But shouldn’t it also referr to the application directory (i.e. User/Workspace…) ??

, [2]

  • if you just want to get the absolute path to WEB-INF then [2]
    is fine
    the reason you get that weird path is because Eclipse deploys it as a webapp in a temp directory under .netadata (and not directly at the same place) and so when you’re running it you get that path

not sure. I think Eclipse uses its own root dir as the root for abosolute paths