1. jar
command can also operate on zip files. I usually run jar tvf hello.zip
to quickly view its content, without starting up the WinZip program. jar xvf hello.zip
should also be able to expand the target zip files. I find it hard to memorize Unix zip/unzip
command line options, so I just use jar tvf/jar xvf
instead. For example:
C:\tmp>jar tvf eclipse-SDK-3.2RC7-win32.zip
2. jar tvf
can selectively list table of contents for archive. I used to run jar tvf j2ee.jar | grep javax/servlet/http
to search for servlet classes in j2ee.jar
. Replace grep
with findstr
on Windows. In fact, I don't need grep
or findstr
; I can just run this command:jar tvf j2ee.jar javax/servlet/http
Note that the search criteria are matched against the beginning of all entries in jar file. It uses String.startsWith(what)
rather than String.contains(what)
. So this command jar tvf j2ee.jar ejb
will not return any matching entries, though jar tvf j2ee.jar javax/ejb
will return all ejb classes. The search is also case-sensitive.If you want case-insensitive search, or match by any parts (not just the beginning) of entries, you still need to use
jar tvf my.jar | grep -i aNynAmE
3. You can extract selected entries from a jar file. For instance, if you only want to view the meta-inf/manifest.mf file, you can
C:\Sun\AppServer\lib>jar xvf j2ee.jar META-INF/MANIFEST.MF
inflated: META-INF/MANIFEST.MF
Or using a backslash instead of a forward slash:C:\Sun\AppServer\lib>jar xvf j2ee.jar META-INF\MANIFEST.MF
inflated: META-INF/MANIFEST.MF
The entry names are case sensitive, and so the following will not extract anything:C:\Sun\AppServer\lib>jar xvf j2ee.jar meta-inf/manifest.mf
Of course, you can always double-click the entry to view it in WinZip, fileroller, or other tools.4. You can choose not to have manifest file when creating a jar file, using M
option:
jar cvfM no-meta.jar A.class
adding: A.class(in = 405) (out= 279)(deflated 31%)
Note: it's upper-case M. Lower-case m has a different meaning.5. You can specify your own manifest file when creating a jar file, using
m
option:jar cvfm my-meta.jar my-meta-inf\my.mf A.class
added manifest
adding: A.class(in = 405) (out= 279)(deflated 31%)
The option used here cvfm
tells the jar command that the destination file (f) will come next and then custom manifest file (m). I can also specify them in a different order:jar cvmf my-meta-inf\my.mf my-meta.jar A.class
added manifest
adding: A.class(in = 405) (out= 279)(deflated 31%)
It's lower-case m. Upper-case M has a different meaning.6.META-INF/MANIFEST.MF file in source files is always ignored.
jar cvf ignore.jar A.class META-INF
added manifest
adding: A.class(in = 405) (out= 279)(deflated 31%)
ignoring entry META-INF/
adding: META-INF/LICENSE.txt(in = 2657) (out= 1185)(deflated 55%)
ignoring entry META-INF/MANIFEST.MF
When it comes to manifest files for new jar file, you only have 3 options:- Do not specify any manifest-related options and use the default MANIFEST.MF
- Use option
M
not to include a manifest file - Use option
m
to use a custom manifest file. Inside the jar file, this file will always be named MANIFEST.MF under META-INF directory. Outside of the jar file, this custom manifest file can be anywhere and have any name.
cvfm
option, this custom manifest file is ignored and a default MANIFEST.MF is included.8. jar
command options can start with optional -. jar tvf a.jar
is the same as jar -tvf a.jar
9. You would usually use relative paths for source files, relative to the current directory. These relative paths will be preserved inside the target jar file. For example, jar cvf \tmp\b.jar com\javahowto\test\
will create the directory tree com\javahowto\test\
inside the target jar, and include all files under
If you use absolute paths for source files, jar
will copy the absolute paths inside the jar file, which is not what you want. For example,
C:\tmp>jar cvf a.jar C:\tmp\A.class
added manifest
adding: C:/tmp/A.class(in = 405) (out= 279)(deflated 31%)
10. If source files are not located in the current directory, you can use -C
option to tell jar
command to implicitly change to another directory and then include files there. For example,C:\ws\nb\scrap\dist>jar cvf hello-world.jar -C ..\build\classes com\javahowto\test
11. If you use -C ../build/classes
option and want to include all files under ../build/classes
, use . (dot) to represent all files there. Note that you can't use *, which will resolve by OS to all files to the current directory. For example,C:\ws\nb\scrap\dist>jar cvf hello-world.jar -C ..\build\classes .
The following example (using -C and *) will ignore -C option and instead include all files in the current directory, which is not what we want:C:\ws\nb\scrap\dist>jar cvf hello-world.jar -C ..\build\classes *
..\build\classes\com\hello-world.jar : no such file or directory
added manifest
adding: scrap.jar(in = 20487) (out= 7472)(deflated 63%)
12. There are 3 types of paths in jar
command:- path to the destination jar file, either relative or absolute path is fine. In fact, any format is ok as long as it can be correctly resolved by the OS
- path to the manifest file, if
m
option is present. Either relative or absolute path is fine. The same as destination file. - multiple paths to source files. They should be relative to the current directory, unless
-C
option is present. In that case, all source files should be relative to the value of-C
option.
-C
option, I'd suggest you always cd into the parent directory of all source files, and then run jar
command.13. It is not possible to include source files from multiple different parent directories. But you can always first copy them into a common parent directory. Jar task in Apache Ant is more flexible and can accommodate almost all use cases.
14. In JDK 6 or newer version, you can use
e
option to specify an entry-point class (Main-Class in META-INF/MANIFEST.MF) for self-contained applications packaged in a jar file. See this post for details.
No comments:
Post a Comment