Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a JAR with only the class files needed by a given class

Tags:

java

jar

I have a directory full of hundreds of class files that have been constructed by a previous compilation. Let's say I have a class which only depends upon a small subset of those generated class files. Is it possible to create a JAR which only has the dependencies for the given class?

EDIT: Please note that I am not speaking of the library level dependencies (i.e. JARs). When I refer to dependencies above, I am referring to the sort of dependency that results from class A calling class B. Perhaps an example would be good. Imagine I have the following classes in my project.

public class A {
    B bField;
}

public class B {
    C cField;
}

public class C {
    B bField;
}

Now imagine I want to build a JAR with class B, then the JAR would also need to include the class file for C because the one depends upon the other. If I wanted to build a JAR from class A, then all three classes would be included. Is there a way to examine this dependency chain and build a JAR with the result?

like image 823
Viper Bailey Avatar asked Jan 19 '26 12:01

Viper Bailey


1 Answers

Several products can do this, including ProGuard.

The danger is that without exhaustive run-time analysis, or a good understanding of your code and any frameworks it uses, some classes may be missed if they've instantiated via reflection. Plugin systems, dependency injection, scripting, and so on can all interfere with the accuracy of static analysis.

like image 132
Dave Newton Avatar answered Jan 22 '26 01:01

Dave Newton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!