dependencies : Java Glossary

go to home page D words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish by Roedy Green ©1996-2009 Canadian Mind Products
index page for letter ⇒ punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (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 a
    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 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.
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.

CMP homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.62] The information on this page is for non-military use only.
You are visitor number 9,192. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/dependencies.html J:\mindprod\jgloss\dependencies.html