Skip to main content

How to get the current path of my web application

3 replies [Last post]
Joined: 2007-09-03


i need to get the current path of my war file from glassfish in java source code, how can i do that ??

I use jsf application and i need to load a file located in my root folder of my war

with this code i get only the context name :/

FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();

String path = request.getContextPath();

but how can i load my file

thanks a lot

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I had a situation where i needed to load a file from the domain's docroot
(i.e. glassfish/domains/domain1/docroot)

This code provides the path of the docroot, and then you can just append the
file name:

* Get path of the docroot, regardless of where project is deployed
* @return docroot path
private static String getDocRoot() {
File docroot1 = new File("../docroot");

try {
docroot1 = docroot1.getCanonicalFile();
} catch (IOException e) {
docroot1 = docroot1.getAbsoluteFile();
return docroot1.getPath() + "\\";
View this message in context:
Sent from the - glassfish users mailing list archive at

To unsubscribe, e-mail:
For additional commands, e-mail:

Joined: 2004-12-01

Have you tried using ServletContext.getRealPath()?

Joined: 2007-09-03

thanks its done with

FacesContext context = FacesContext.getCurrentInstance();
ServletContext sc = (ServletContext) context.getExternalContext().getContext();

String path = sc.getRealPath("myfile"); when myfile is located in the root of my webapplication