Reputation: 41
I need to write new module, which is able to get all application's jar in runtime.
I try do it with getting inforimation with Classloader.getSystemClassloader()
, but catalina.bat
in Tomcat override variable of classpath.
So, next idea was to recursively iterate through all the target folders and look for a 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 path to my war, which I added to application. But I need the path to the application itself.
So, have you any idea?
Upvotes: 4
Views: 5138
Reputation: 9717
Java EE applications have customized ClassLoader
hierarchy.
public class MainServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) {
ClassLoader effectiveClassLoader = getClass().getClassLoader();
URL[] classPath = ((URLClassLoader) effectiveClassLoader).getURLs();
...
}
}
Upvotes: 4
Reputation: 1413
You want the classloader for your application, not Tomcat. Get the classloader for the servlet - this.getClass().getClassLoader() or MainServlet.getClassLoader() should both work.
Upvotes: 0