dependencies : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

dependencies
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?

  1. You examine the source code chasing the dependency chains seeing who calls what.
  2. 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
    rem capturing Javac verbose output
    rem to get a rough idea of the dependencies
    
    javac.exe -verbose *.java 2>temp.txt
    
    rem -30-
    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.

  3. 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.
  4. 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.
  5. Use a dependency tracking tool like Genjar.
  6. Use the possible dependency tracking abilities of your IDE (Integrated Development Environment).
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.

This page is posted
on the web at:

http://mindprod.com/jgloss/dependencies.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\dependencies.html
Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[3.19.31.73]
You are visitor number