Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtaining a list of all classes used while running a Java application?

Tags:

java

jvm

What is the easiest way to obtain a list of all classes used while running a Java application?

Assume that com.package.Foo.main is invoked by running:

java com.package.Foo

After running the program I'd like to have a list of all classes that have been used while running the program:

cat classes-used.txt
com.package.Foo
com.package.FooHelper
com.otherpackage.SomeClass
java.lang.String
java.util.List
java.util.ArrayList

In this context a class is defined as being used if it the class has been loaded by the class loader during program execution and the class' static block would have been run if such static block had existed.

like image 309
knorv Avatar asked Oct 05 '09 20:10

knorv


People also ask

How do I get all classes in a classpath?

You can get all classpath roots by passing an empty String into ClassLoader#getResources() . Enumeration<URL> roots = classLoader. getResources("");

How can you directly access all classes in the packages in Java?

1) Using packagename.* If you use package.* then all the classes and interfaces of this package will be accessible but not subpackages. The import keyword is used to make the classes and interface of another package accessible to the current package.

What is ClassLoader in Java with example?

The Java ClassLoader is a part of the Java Runtime Environment that dynamically loads Java classes into the Java Virtual Machine. The Java run time system does not need to know about files and file systems because of classloaders. Java classes aren't loaded into memory all at once, but when required by an application.

What is URLClassLoader in Java?

java.net.URLClassLoader. This class loader is used to load classes and resources from a search path of URLs referring to both JAR files and directories. Any URL that ends with a '/' is assumed to refer to a directory. Otherwise, the URL is assumed to refer to a JAR file which will be opened as needed.


2 Answers

Run java with the flag verbose:class

java -verbose:class com.package.Foo

To append it to a file:

java -verbose:class com.package.Foo > classes-used.txt

etc.

It also list the jar where those files are defined:

For instance for this class

public class Test {
     public static void main( String [] args ) {
     }
}

I've got ( among others )

$ java -verbose:class Test 
[Opened /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/classes.jar]
[Opened /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/ui.jar]
[Opened /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/laf.jar]
[Opened /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/jsse.jar]
[Opened /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/jce.jar]
[Opened /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Classes/charsets.jar]
[Loaded java.lang.Object from shared objects file]
[Loaded java.io.Serializable from shared objects file]
[Loaded java.lang.Comparable from shared objects file]
[Loaded java.lang.CharSequence from shared objects file]
[Loaded java.lang.String from shared objects file]
[Loaded java.lang.reflect.GenericDeclaration from shared objects file]
[Loaded java.lang.reflect.Type from shared objects file]
[Loaded java.lang.reflect.AnnotatedElement from shared objects file]
[Loaded java.lang.Class from shared objects file]
[Loaded java.lang.Cloneable from shared objects file]
[Loaded java.lang.ClassLoader from shared objects file]
[Loaded java.lang.System from shared objects file]
like image 86
OscarRyz Avatar answered Sep 26 '22 19:09

OscarRyz


I believe you could also use Jakarta Commons Discovery, with code similar to the following:

ResourceNameIterator classes = new DiscoverClasses().findResourceClasses(Object.class);
while (classes.hasNext()) {
    String className = classes.nextResourceClass().getName();
    Class clazz = classes.nextResourceClass().loadClass();
}
like image 37
Stephen Avatar answered Sep 23 '22 19:09

Stephen