PlsWork
PlsWork

Reputation: 2158

Find classes which are used during compilation in eclipse

I have a package x which contains about 30 classes. The program I am compiling uses 5 classes from package x. How to find the 5 classes which are used during my current compilation at a glance?

My current approach is to inspect the code and see which classes are used/instantiated, this is tedious and slow as I have to manually go through the code. Is there a better approach to find all classes which are used during my current compilation?

I want to press a button and see all classes which are required for the compilation of my program.

Upvotes: 2

Views: 52

Answers (1)

holi-java
holi-java

Reputation: 30676

How about this command:

# which jar file you need analyzing
export JAR_FILE="...";
# export CLASSPATH environment for compiling purpose
export CLASSPATH="...";

javac -verbose *.java 2>&1 \
     | grep -E "^\[loading" \
     | grep $JAR_FILE \
     | grep -Eo "\((.*?)\)";

Example

compiling Main.java:

public class Main {
   public static void main(String[] args) {
      org.joda.time.DateTime dateTime = new org.joda.time.DateTime();
   }
}

with commands below:

export JAR_FILE="joda-time-2.9.9.jar";
export CLASSPATH="joda-time-2.9.9.jar";
javac -verbose *.java 2>&1 \
     | grep -E "^\[loading" \
     | grep $JAR_FILE \
     | grep -Eo "\((.*?)\)";

will outputs the classes that used in joda.jar only:

(org/joda/time/DateTime.class)

Upvotes: 2

Related Questions