7

Suppose there is a jar file named callme.jar

and it's located in several directories,

how to analyse which one of them is used at run-time?

Steve Kuo
  • 61,876
  • 75
  • 195
  • 257
omg
  • 136,412
  • 142
  • 288
  • 348

5 Answers5

30

Invoke the java executable with the -verbose:class argument. This will produce output like:

[Loaded org.apache.log4j.helpers.ThreadLocalMap from file:/C:/.../1.2.14/log4j-1.2.14.jar]
[Loaded org.apache.commons.cli.Option from file:/C:/.../commons-cli-1.2.jar]

matt b
  • 138,234
  • 66
  • 282
  • 345
3

Might not be the easiest method, but you could look at what files the process has open and determine it from that.

If you're on windows you could use Process Explorer to see what files the process has open at any given time, or Process Monitor to watch the filesystem access as it runs. There will be a lot of noise, but you could figure it out from there.

If you're on the Mac I think the built-in Activity Monitor can give you a list of open files. Sadly I don't know the command you'd use in Linux.

Herms
  • 37,540
  • 12
  • 78
  • 101
2

Try this piece of code :

    //Get the System Classloader
    ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();

    //Get the URLs
    URL[] urls = ((URLClassLoader)sysClassLoader).getURLs();
    System.out.println("CURRENT CLASSPATH :");
    for(int i=0; i< urls.length; i++){
        System.out.println(urls[i].getFile());
    }       
    System.out.println("END OF CLASSPATH");
g andrieu
  • 2,081
  • 3
  • 14
  • 10
2
System.getProperty("java.class.path");
rich
  • 989
  • 2
  • 9
  • 17
  • This answer is deceptively correct - because the JVM (or at least Sun's?) will load the class from the first location on the classpath that contains it; in the order of the classpath – matt b Jun 04 '09 at 13:55
  • This would only work if you have control over the code. That might be the case here, but I'm not sure. – Herms Jun 04 '09 at 13:58
0

It is not that easy, to know whether piece of code from JAR is being used is only possible when your code calling that jar is being executed (i.e. being covered). Till the point your code isn't covered it is difficult to know whether jar on your application class path is being used or not.

For this you might need to right good junit test cases which can cover you entire code and then you can use Class Dependency Analyzer tools.

It is kind of try and see thing. You can try removing JARs one by one and check whether application do works or not but junit cases are the best options to do this examination.

Avinash Kadam
  • 486
  • 7
  • 10
  • With an exhaustive (unit) test suite as this answer suggests, coupled with JVM option `-verbose:class` (not with dependency analyzer nor with manually removing jar-s) you have a good change even in complex setups like web apps that have a deep hierarchy of class loaders with potentially custom loading policies. – lcfd Oct 15 '18 at 12:41