Skip to content

Instantly share code, notes, and snippets.

@mcrisc
Created January 6, 2010 18:25
Show Gist options
  • Save mcrisc/270498 to your computer and use it in GitHub Desktop.
Save mcrisc/270498 to your computer and use it in GitHub Desktop.
/**
* Returns the parent directory of a class being run by a web
* container, such as Tomcat. Useful for generating a
* value for iReport's SUBREPORT_DIR parameter.
*
* If using Struts 1.x, call this method this way:
<code>
getRealPath(getServlet().getServletContext(), YourClassName.class);
</code>
*/
private String getRealPath(ServletContext context, Class clazz) {
StringBuilder path = new StringBuilder(context.getRealPath("WEB-INF"));
path.append(File.separator).append("classes").append(File.separator);
final Package clazzPackage = clazz != null ? clazz.getPackage() : null;
if (clazzPackage != null) {
for (String component : clazzPackage.getName().split("\\.")) {
path.append(component).append(File.separator);
}
}
return path.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment