Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the classpath of a web app

When I'm working on my web app in Eclipse, I can search all the classes and JAR files with

String cp = System.getProperty("java.class.path");

but when I deploy on Tomcat or Jetty, I get nothing.

I want to find all *.properties files which are located in the webapp (either in the JAR or, if exploded, in folders).

String cp = System.getProperty("java.class.path");
String pathSep = File.pathSeparator;
String[] jarOrDirectories = cp.split(pathSep);
for (String fileName : jarOrDirectories) {
        File file = new File(fileName);
        if (file.isFile()) {
                logger.debug("From jar "+file.getName());
                loadFromJar(file.getPath());
        } else {
                logger.debug("From folder "+file.getName());
                listFolder(file);
        }
}

This way, running the web application in Eclipse gives me all JAR files and all class folders, but not when I deploy the application in Tomcat.

It would be enough if I could just get the path to WEB_INF. From there, it's easy.

like image 612
Zemzela Avatar asked Feb 24 '23 16:02

Zemzela


1 Answers

If someone needs the answer, I found a very obvious answer.

getServletContext().getRealPath("/WEB-INF")

Then when you receive the real path you can easily search through the folders and JARs.

like image 182
Zemzela Avatar answered Mar 05 '23 18:03

Zemzela