I've yet to hear anyone ask this question about a problem I used to have and finally wrote a program to solve.
[I}How do you insure that a jar file contains all the needed class files? [/I] I have several utility classes that exist on my CLASSPATH that I use to develop various programs I use. I acccess them via the CLASSPATH during a program development phase, and put them into a jar file with the classes for project I'm working on. I make a batch file with a jar command to build the jar file and usually forget to include some class which then causes the program to fail when I click on the jar file to start it. I'm working on a Win98 system. Because the default for "starting" a program in a jar file is javaw, you don't see any error messages. To find the problem, you open a DOS window and enter: java -jar <jarfilename> and then are able to see the error message about a missing class file.
Does anyone else have this problem? How do/did you solve it?
Actually, the solution is to not use a global CLASSPATH (as some of the rabid, foaming-at-the-mount anti-global-classpath zealots will soon be here to tell you!) Use Ant (or make, if you're a retro guy) and then use the same classpath in Ant to compile as you use in Ant to build your jar. Because the same list of classes are used for both, you can't make a mistake!
Thanks for the response. I'll have to get a copy of Ant and look at it. A question about how Ant works: For Ant to solve my problem it would have to keep track of which of my utility classes I use in a program I'mn writing and also what classes each of those utility classes use. It can then build a jar file with all and only the class files needed for the program to work. If I have to manually enter which of my utility classes I'm using, I can just as easily create the batchfile with the jar commmand (and of course leave out some class in either case).
My utility classes are mostly in one package, say the same idea as java.util.*
author and iconoclast
The approach I've outlined will create a complete jar, but not a minimal jar. Unless you're writing an applet (so that download size is important) people generally don't bother trying to do what you're asking about. They just include a library all of a piece.
But in any case, there are tools that will analyze a jar and remove unused classes. I know that commercial obfuscators like Dash-O Pro do this; I'm not sure if any of the equivalent open-source tools like Retroguard have this capability or not, but it's worth a look.