Autojar helps creating jar files of minimal size from different inputs like own classes, external archives etc. It starts from a given class (e.g., an applet), recursively searches the bytecode for references to other classes, extracts these classes from the input archives, and copies them to the output. The resulting archive will only contain the classes you really need. Thus you can keep the size and loading time of applets low or make applications independent of installed libraries.In a similar way, autojar can search directories and archives for other resources (like image files), extract them and copy them to the output.
Note that the program can't tell which classes will be loaded dynamically (by Class.forName()). If you know these classes in advance, you can simply provide them on the command line. If you don't, however, autojar may be not suitable.
Autojar uses the excellent bcel package (http://jakarta.apache.org/bcel/).
![]()
Version 1.2.1: Some bugs fixed.
New options: -e, -q, -D.
Wildcards ? and * allowed in search paths.
Improved dependency checking.
Missing files are reported.Version 1.1: Along with option -p, a search path for non-class files may be supplied (e.g., .gif files). Alternatively option -b ("both") tells autojar to use the class path for non-class files, too.
![]()
Program parameters may either be given on the command line or read from a text file.
In the second form, each line of the file is considered a program parameter.autojar [option...] [file...]
autojar @file
Option Meaning -b Use classpath for non-class files. -c classpath Class path in the usual format (system dependent), used to search for class files. The name part of each component may contain wildcards * and ?. Example: If directory
/foo
contains the archivesa.jar
,b.jar
,c.jar
, the expression-c /ah/oh:/foo/*.jar
expands to-c /ah/oh:/foo/a.jar:/foo/b.jar:/foo/c.jar
. Unix users will have to quote the argument in order to avoid expansion by the shell.The class path is initialized by the value of the environment variable
$CLASSPATH
(system property "java.class.path"), if set. The default is ".". Multiple instances are merged into one:-c /usr/foo -c /usr/bar
is the same as-c /usr/foo:/usr/bar
.-D Debug mode, shows more detailed error output. Not normally used. -e Search all jars in all Java extension directories (system property "java.ext.dirs"). Without this option, archives in $JAVA_HOME/jre/lib/ext that are to be searched must be specified explicitely. -h Print help and exit. -m manifest Path of the manifest file. The class in the Main-Class entry will be treated like classes given on the command line. -o outfile Output file, mandatory. -p searchpath Search path for non-class files, same format as class path. -q Run silently. Don't issue warnings. -v Verbose output. -x prefix Exclude prefix. Directories and archives in the search path whose path names start with prefix are ignored. Multiple instances are allowed. -- End of options. All remaining parameters are regarded non-options, even if starting with - . ![]()
An arbitrary number of file or directory paths may be supplied. What happens depends on the file type:
- Class files (suffix
.class
): If the path names an existing file, this file is added to the archive. Otherwise, if it is a relative name, the class path is searched. If the file can be found, it is added to the archive. Autojar then scans the bytecode, tries to find all classes that this class uses (directly or indirectly), extracts the corresponding files from their archives and adds them, too.- Non-class files: If the path denotes an existing file, it is added to the output archive. If it is a relative path name and a search path for non-class files has been provided (by -p or -b), this path is searched. If the file can be found, it is extracted and added to the output.
- Directories: Directories are added recursively, that is, including all files and subdirectories they contain.
- Jar files: The contents of a jar file are copied to the output, except for the "META-INF" subtree. All classes it contains are then scanned for references to other classes, like described above.
- The special sequence
-C dir file
has the same meaning as in thejar
command: The working directory is changed temporarily to dir, the parameter file is evaluated, and then the working directory is changed back again. file may contain wildcards. A . (dot) means the complete content of the current directory.If
-C
is the first non-option parameter, it must be prefixed by--
to tell autojar it is not an option.Classes from the Java runtime library will never be included in the output.
At exit, if Option -q is not present, autojar prints a list of missing files.
![]()
Example 1: An applet consists of a main classMyApplet
plus several other classes, all in the directoryclasses
. Additionally some classes from the archive/local/lib/foo.jar
are used, and all image files in the directoryimages
, which are referred to as"x.gif"
,"y.gif"
etc. (that is, without the directory part). The jar file may then be created like this:
If you refer to your image file asautojar -vc classes:/local/lib/foo.jar -o myapplet.jar MyApplet.class -C images '*.gif'
"images/x.gif"
,"images/y.gif"
, replace-C images '*.gif'
by'images/*.gif'
.Example 2: An application consists of a main class
Magic
and 199 additional classes, mixed up with classes from other projects, sources, and all kinds of junk in your working directory (not a particularly good idea, of course). The filemagic.mf
contains the entryMain-Class: Magic
. Now the call
autojar -o magic.jar -m magic.mf
creates the archive
magic.jar
, saving you from the need to supply all 200+ names individually.Example 3: An application with main class
com.wow.MyApp
dynamically loads an driver by callingclass.forName("org.blah.Driver")
. You know that the driver uses classes from/usr/share/java/foo.jar
.
Classes used inautojar -o myapp.jar -c /usr/share/java/foo.jar classes/com/wow/MyApp.class org.blah.Driver.class
Driver
are searched forExample 4: An Applet wants to load the images
toolbarButtonGraphics/general/Cut16.gif
andtoolbarButtonGraphics/general/Paste16.gif
from its own jar file:JButton cutButton = new JButton(new ImageIcon(getClass().getResource("toolbarButtonGraphics/general/Cut16.gif"))), pasteButton = new JButton(new ImageIcon(getClass().getResource("toolbarButtonGraphics/general/Paste16.gif"))), /* ... */The images are contained in the archive/usr/local/jlfgr-1_0.jar
, downloaded from Sun. To avoid extracting and re-inserting them manually, you could write:autojar -o myapplet.jar -p /usr/local/jlfgr-1_0.jar MyApplet.class \ toolbarButtonGraphics/general/Cut16.gif toolbarButtonGraphics/general/Paste16.gifIf you need a lot of these images and don't care about file size, you could also writeautojar -o myapplet.jar /usr/local/jlfgr-1_0.jar MyApplet.classThis includes the whole content ofjlfgr-1_0.jar
in your archive.![]()
If you want to compile autojar yourself, first get the bcel package. Make aclasses
directory, putbcel.jar
in the classpath and compileAutojar.java
:(replacejavac -d classes -classpath classes:/usr/share/java/bcel.jar -sourcepath . de/uni_hamburg/eggink/autojar/Autojar.java
/usr/share/java
by the directory containingbcel.jar
on your system). Now you can run autojar, but don't forget to includebcel.jar
in the class path:If you prefer a self-contained archive you can create it by applying autojar to itself:java -classpath classes:/usr/share/java/bcel.jar de.uni_hamburg.eggink.autojar.Autojar parameters...
java -classpath classes:/usr/share/java/bcel.jar de.uni_hamburg.eggink.autojar.Autojar -vc /usr/share/java/bcel.jar:classes -o autojar.jar -m autojar.mf
![]()
Most likely.Please report bugs, wishes etc. to:
Bernd.Eggink@rrz.uni-hamburg.de
.![]()