Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find minimal necessary java classpath

Is there a tool to detect unneeded jar-files?

For instance say that I have myapp.jar, which I can launch with a classpath containing hibernate.jar, junit.jar and easymock.jar. But actually it will work fine using only hibernate.jar, since the code that calls junit.jar is not reachable.

I realize that reflection might complicate things, but I could live with a tool that ignored reflection. Except for that it seems like a relatively simple problem to solve.

If there is no such tool, what is best practices for deciding which dependencies are needed? It seems to me that it must be a common problem.

like image 288
Rasmus Faber Avatar asked Sep 16 '08 19:09

Rasmus Faber


People also ask

What should classpath be set to?

The default value of CLASSPATH is a dot (.). It means the only current directory searched. The default value of CLASSPATH overrides when you set the CLASSPATH variable or using the -classpath command (for short -cp). Put a dot (.)

How does Java search the classpath?

By default, javac and javadoc search the user class path for both class files and source code files. If the -sourcepath option is specified, javac and javadoc search for source files only on the specified source file path, while still searching the user class path for class files.

How do I know if a jar is in classpath?

A pragmatic way: Class. forName("com. myclass") where com. myclass is a class that is inside (and only inside) your target jar; if that throws a ClassNotFoundException , then the jar is not on you current classpath.


1 Answers

This is not possible in a system that might use reflection.

That said, a static analysis tool could do a pretty good job if you don't use ANY reflection.

like image 149
Bill K Avatar answered Sep 17 '22 04:09

Bill K