创建jar文件

时间:2009-10-11 13:59:32

标签: java eclipse jar

有人可以解释一下如何包装jar文件。我的源文件夹有各种子文件夹和图像文件。你可以从下面的url看到解释源文件夹结构的图像 http://img63.imageshack.us/gal.php?g=capturev.png。 当使用eclipse export jar函数创建jar时,它只包含类文件,但我想为源文件夹中的所有内容创建jar文件。第三张图片显示了jar创建时获得的窗口。它没有其他文件夹和图像可供选择。

3 个答案:

答案 0 :(得分:5)

查看Sun jar tutorial

虽然您可以从命令行运行jar,但我建议您使用Ant和Ant Jar task。它将为您提供更多控制如何构建jar文件以及包含在其中的内容。

e.g。

  <jar destfile="${dist}/lib/app.jar"
       basedir="${build}/classes"
       excludes="**/Test.class"
  />

会将classes目录中的所有内容构建到.jar文件中,但不包括Test类。

答案 1 :(得分:2)

您可以使用jar命令从命令行执行此操作:

Usage: jar {ctxui}[vfm0Me] [jar-file] [manifest-file] [entry-point] [-C dir] files ...
Options:
    -c  create new archive
    -t  list table of contents for archive
    -x  extract named (or all) files from archive
    -u  update existing archive
    -v  generate verbose output on standard output
    -f  specify archive file name
    -m  include manifest information from specified manifest file
    -e  specify application entry point for stand-alone application 
        bundled into an executable jar file
    -0  store only; use no ZIP compression
    -M  do not create a manifest file for the entries
    -i  generate index information for the specified jar files
    -C  change to the specified directory and include the following file
If any file is a directory then it is processed recursively.
The manifest file name, the archive file name and the entry point name are
specified in the same order as the 'm', 'f' and 'e' flags.

Example 1: to archive two class files into an archive called classes.jar: 
       jar cvf classes.jar Foo.class Bar.class 
Example 2: use an existing manifest file 'mymanifest' and archive all the
           files in the foo/ directory into 'classes.jar': 
       jar cvfm classes.jar mymanifest -C foo/ .

但就个人而言,我会为此目的使用像Ant或Maven这样的东西。

答案 2 :(得分:0)