How to get all jar from tomcat classpath application?

I need to write a new module that can get all jar applications at runtime.

I'm trying to do this with getting information using Classloader.getSystemClassloader(), but catalina.batin Tomcat overrides the classpath variable.

So, the next idea was to recursively iterate over all the target folders and look for the lib folder . In my module, I wrote:

public class MainServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        File currentDir = new File(getServletContext().getRealPath("/")); // current directory
        System.out.println("Cur dir: " + currentDir);
        displayDirectoryContents(currentDir);
    }
}

I got this result:

Cur dir: C: \ Java \ apache-tomcat-7.0.56 \ webapps \ jar

This is the path to my war that I added to the app. But I need a path to the application itself.

So do you have any ideas?

+4
2

Java EE ClassLoader.

public class MainServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) {
        ClassLoader effectiveClassLoader = getClass().getClassLoader();
        URL[] classPath = ((URLClassLoader) effectiveClassLoader).getURLs();
        ...
    }
}
+4

, Tomcat. classloader - this.getClass(). GetClassLoader() MainServlet.getClassLoader() .

0

Source: https://habr.com/ru/post/1584625/


All Articles