I need to get jarNames of the libraries used from my web application on Tomcat and Websphere servers.
I have this code:
public void libs(ActionEvent e){
System.getProperty("java.class.path");
try {
Field ucp = this.getClass().getClassLoader().getClass()
.getSuperclass().getDeclaredField("ucp");
ucp.setAccessible(true);
Object valueUcp = ucp.get(this.getClass().getClassLoader());
Field loaders = valueUcp.getClass().getDeclaredField("path");
loaders.setAccessible(true);
ArrayList collection = (ArrayList) loaders.get(valueUcp);
} catch (Exception e) {
e.printStackTrace();
}
}
It works on Tomcat, but not on websphere.
PS. Sorry my bad english.
There is no portable API to get this information. On WebSphere Application Server, ClassLoader.toString typically contains the full class path for diagnostic purposes, but I would definitely not recommend attempting to "parse" the output. I wouldn't recommend using reflection on Tomcat either: it might work for current Tomcat versions, but there's no guarantee the class loader implementation won't change and break your application.