dependencies : Java Glossary
When you build a jar, you naturally include all the class files and resources in it
from the package. However, if your code uses classes from other packages,
jar.exe is not smart enough to include them. You have to
explicitly specify them. What makes matters worse, is you not only need all the
classes your code uses, but all the code that code indirectly uses. If you forget a
class, you won’t get an error message right away. Only when you are running
code, when you eventually get around to exercising those classes, only then do you
get the dreaded NoClassDefFoundError.
How do you discover this chain of dependencies and the list of classes you need to
include in your jar?
If you distribute source code in a zip, you need to distribute the corresponding
java source and class files for all the dependencies you added to your jar.
- You examine the source code chasing the dependency chains seeing who calls
- Let Javac help you. Delete all class files in the universe of possibilities.
Then compile your base package. Then look to see which classes Javac.exe compiled in other packages. You can do
javac.exe -verbose *.java 2>temp.txt
to see what classes Javac compiled or looked at. It will cover the classes being
compiled and classes your classes reference, but the listing won’t go
futher to look at which classe those classes need. The compiler does not need to
to be so recursive just to generate code.
These are the classes you will need to include in your jar. Erase all class
files in the universe again and repeat compiling some other package to get its
list of dependencies. So long as you have a good idea of what packages you use,
this is pretty quick. You can even do a dir *.class to
get a list of classes you can paste into your forjar.list file. This sort of works because javac will compile
source is other packages if it cannot find a class file. If it can find a class
file, no matter how out of date, it will use that without recompiling it. It
might find one in a jar on the classpath, in the same directory as the source, or
any other place accessible via the classpath.
- You make a brave stab at composing a list of dependencies, then exercise the
code and look for NoClassDefFound. Add that to the list
and start over. The problem is, you never know if there yet one more, that does not
show up except under exceptional circumstances.
- Compile the code with JET. On each
compile, JET (Just Enough Time) will warn of you one missing dependency. Add that
missing class to the jar and repeat the JET
compile.. This is tedious, but in the end when JET
says your jar is ok, you can be sure there are no more dependencies you
overlooked. The catch is, JET
has no idea
what you are up to with Class. forName. So it can miss dependencies you call that way.
- Use a dependency tracking tool like Genjar.
- Use the possible dependency tracking abilities of your
IDE (Integrated Development Environment).