Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine which classes are used by a Java program?

Is there any tool that lists which and when some classes are effectively used by an app or, even-better, automatically trims JAR libraries to only provide classes that are both referenced and used?

like image 634
Camilo Díaz Repka Avatar asked Jan 24 '09 04:01

Camilo Díaz Repka


People also ask

How do you identify a class in Java program?

Example 1: Check the class of an object using getClass() In the above example, we have used the getClass() method of the Object class to get the class name of the objects obj1 and obj2 . To learn more, visit Java Object getClass().

How do you know what class an object belongs to?

You can get the class object of an instance via getClass() . And you can statically access a specific class via ClassName. class . In the above example, the condition is true if a is an instance of X , but not if a is an instance of a subclass of X .

What is a class in Java?

A class is a blueprint in the Java programming language from which an individual object can be built. In Java, we may declare a class by using the class keyword. Class members and functions are declared simply within the class. Classes are required for the creation of Java programs.

How to get the class name of an object in Java?

To understand this example, you should have the knowledge of the following Java programming topics: In the above example, we have used the getClass () method of the Object class to get the class name of the objects obj1 and obj2. To learn more, visit Java Object getClass ().

How do you declare a class in Java?

In Java, we may declare a class by using the class keyword. Class members and functions are declared simply within the class. Classes are required for the creation of Java programs. The object-oriented paradigm (OOP) allows users to describe real-world objects.

How to use multiple classes in a Java program?

Using multiple classes in a Java program. A Java program can contain any number of classes. Following Java program comprises of two classes: Computer and Laptop. Both classes have their constructors and a method. In the main method, we create objects of two classes and call their methods. class Computer {. Computer() {.


3 Answers

Yes, you want ProGuard. It's a completely free Java code shrinker and obfuscator. It's easy to configure, fast and effective.

like image 28
Lawrence Dol Avatar answered Sep 20 '22 22:09

Lawrence Dol


Bear in mind that, as proven by the halting problem, you can't definitely say that a particular class is or isn't used. At least on any moderately complex application. That's because classes aren't just bound at compile-time but can be loaded:

  • based on XML config (eg Spring);
  • loaded from properties files (eg JDBC driver name);
  • added dynamically with annotations;
  • loaded as a result of external input (eg user input, data from a database or remote procedure call);
  • etc.

So just looking at source code isn't enough. That being said, any reasonable IDE will provide you with dependency analysis tools. IntelliJ certainly does.

What you really need is runtime instrumentation on what your application is doing but even that isn't guaranteed. After all, a particular code path might come up one in 10 million runs due to a weird combination of inputs so you can't be guaranteed that you're covered.

Tools like this do have some value though. You might want to look at something like Emma. Profilers like Yourkit can give you a code dump that you can do an analysis on too (although that won't pick up transient objects terribly well).

Personally I find little value beyond what the IDE will tell you: removing unused JARs. Going more granular than that is just asking for trouble for little to no gain.

like image 186
cletus Avatar answered Sep 18 '22 22:09

cletus


You might try JarJar http://code.google.com/p/jarjar/

It trims the jar dependencies.

like image 35
Ruggs Avatar answered Sep 18 '22 22:09

Ruggs