6

I am trying to figure out which JAR libraries, out of a total of 83 JARs, are actually being used by a group of 12 Java EE/Spring projects. All JARs are under one EAR. Development is done in Eclipse and the build tool is Ant. All projects run on WebSphere Application Server 6.0.

This question: How to figure out used JARs?

points to this one: How to analyse which jar file is used in a Java program?

which recommends invoking the java executable with the -verbose:class argument, but if this would be useful here, how do I do this in the case where the projects are running on my local WAS 6.0?

This question seems to point more in the right direction: How to find which jars and in what order are loaded by a classloader?

However, I'm not familiar with how a class loader works, how to tell what my class loader instance is (a reply indicates that URLClassLoader has a getURLs() method that provides access to a list of jars/directories), and if my class loader instance is not URLClassLoader, where do I go from there?

Community
  • 1
  • 1
roark
  • 792
  • 1
  • 12
  • 29

4 Answers4

4

Turn on verbose class loading (as suggested by dims)

Look at the admin console and you will see a class loader viewer which shows you the required information.

Class loader has a standard hierarchy that it uses to load the classes.

Have a look at this: http://www-01.ibm.com/support/docview.wss?uid=swg27023549&aid=1

This presentation give you a very good intro to how things work in the WebSphere environment.

HTH

Manglu

Manglu
  • 10,744
  • 12
  • 44
  • 57
  • Thanks, Class Loader Viewer appears to be a good starting point for this. WebSphere admin console -> Troubleshooting -> Class Loader Viewer. The only thing is the `-verbose:dynload` doesn't appear to work on WAS 6.0, but I'm not sure I really need it. – roark Apr 23 '12 at 20:24
  • There is a flag in the admin console to turn on verbose class loading at the JVM. You can check that and that should work for all versions of WAS – Manglu Apr 24 '12 at 00:02
2

For WebSphere 6.1, you can use the "-verbose:dynload" option (see page 52 of the WebSphere Application Server V6.1: Classloader Problem Determination Redbook). You will see the output in native_stderr.log

  • We're using WebSphere 6.0 and this option doesn't appear to work on 6.0. The server starts but initialization fails and the error logged is: `Could not create the Java virtual machine. [ JVMCI129: Unrecognized verbose option: -verbose:dynload ]`. I had to manually edit the server.xml file to remove this option to get back to being able to start the server. – roark Apr 23 '12 at 20:17
1

You could try using something like http://www.jboss.org/tattletale. This can tell you which jars are not being used.

Alex Barnes
  • 7,174
  • 1
  • 30
  • 50
  • If I understand correctly, since Java loads classes dynamically (in the answer by @maximdim below), I might run into an issue where jars aren't referenced but are loaded in a code branch. The [Unused Jar](http://docs.jboss.org/tattletale/userguide/1.2/en-US/html_single/#unusedjar) report indicates that a jar might still be referenced through Java Reflection or through metadata. If I run my projects and am reasonably sure all code was executed, can this tool show all jars that were loaded during that run? – roark Apr 20 '12 at 15:34
  • As you say there are some exclusions to what it's capable of. I'm not sure if it can report on all used jars, it only mentions un-used jars. – Alex Barnes Apr 20 '12 at 16:06
1

Generic answer to this question could be tricky. As Java loads classes dynamically some jars/classes might not be loaded unless all functionality of your program is exercised (think about Class.forName("foo") as trivial example). Therefore any kind of static dependency analysis might not present 100% complete picture, you might have to analyze running application as it executes, preferably hitting all code branches.

As a rough first approximation, to see if there are jars that not being opened at all, you might try to look at list of open files for webSphere process, as reported by your OS ('lsof' on Unix) when your application is running. If jar is not listed as opened it's pretty good indication that it's not being loaded/used but reverse not necessary true as Websphere could scan jars for annotations etc.

maximdim
  • 8,041
  • 3
  • 33
  • 48
  • What is the difference between a jar being opened and a jar being loaded/used? Say I find a jar is not being opened, wouldn't it still be possible that it's opened when the branch of code needing that jar is executed? – roark Apr 20 '12 at 15:24
  • Correct. That's why I said that you need to make sure that all branches of code are executed, as much as you can. – maximdim Apr 20 '12 at 19:18