【问题标题】:How can I add files to a Jar file?如何将文件添加到 Jar 文件中?
【发布时间】:2020-06-21 13:55:40
【问题描述】:

我想将以前从其他文件(已经完成)提取的一系列文件添加到 jar 中。这些文件将覆盖 JAR 中的文件。最有效的方法是什么? 我需要它快。 谢谢!

【问题讨论】:

    标签: java jar


    【解决方案1】:
    jar -u file.jar file1 file2 file3 ...
    

    【讨论】:

    • JAR 只不过是 ZIP,所以 - zip -ur file.zip dir-fith-files/
    • 它适用于 linux / mac os / windows,但您需要为您的操作系统安装它。
    • 为什么您希望您的客户在 JAR 文件中添加一些内容?让他们指定包含文件的文件夹并在需要时阅读它们。
    • @zyngawow,如果打算使用它的人无法处理命令行,他们应该能够使用他们喜欢的任何 zip 应用程序。我怀疑如果非技术用户打算以任何方式(CLI 或 GUI)摆弄存储在 jar/zip 文件中的设置,他们无论如何都会抱怨。
    • @zyngawow 可以用上面解释的方法做到这一点。为什么其他人必须重复它?
    【解决方案2】:
    jar -uf my.jar file1 file2...
    jar -uf my.jar dir/
    

    或混合

    jar -uf my.jar file dir/
    

    【讨论】:

    • 我不知道为什么这个答案投票率很低。当我尝试在 bash 脚本中使用 jar -u 时,我不得不按 Ctrl + C 以使脚本继续(出于某种莫名其妙的原因)。添加-f 修复它。
    【解决方案3】:

    JAR 文件是 ZIP 文件,请记住。

    只需使用一些 ZIP 库。

    【讨论】:

    • 如果你只是想不时地扔一些文件,那么我在 Windows 8.1 上所做的就是将文件的扩展名从 jar 重命名为 zip。然后我可以使用 Windows 资源管理器导航到文件并根据需要添加/删除。完成后把它放回罐子里,给自己喝杯啤酒。
    【解决方案4】:

    只是为了补充现有答案,至少有一种特殊情况:所谓的可执行 JAR 文件。如果你添加另一个 JAR 文件作为依赖项——无论你使用 jar 还是 zip——它都会抱怨嵌入的文件被压缩:

    Caused by: java.lang.IllegalStateException: Unable to open nested entry 'BOOT-INF/lib/file.jar'. It has been compressed and nested jar files must be stored without compression. Please check the mechanism used to create your executable jar file
    

    对此的解决方案是使用0 选项来jar:

    jar uvf0 myfile.jar BOOT-INF/lib/file.jar
    

    普通的类文件不需要这个。

    【讨论】:

      【解决方案5】:
       zip file.jar file1 file2 file3 
      

      适用于 Mac Os 10.7.5

      【讨论】:

        【解决方案6】:
        //Add a file in jar in a particular folder
        jar uvf <jar file name> <file to be added> <folder name inside jar>
        

        【讨论】:

        • 如何将文件添加到 jar 文件的根目录 - &lt;folder name inside jar&gt; 使用什么。我的用例是src/config.properties。我试过jar --update --file=../demo.jar --main-class=demo.App src/config.properties &lt;???&gt;
        【解决方案7】:

        扩展现有答案,我发现 -C jar 选项在添加位于自己文件夹中的文件并且您将其路径展平时非常有用。

        $ jar uf jar-file -C /path/to/my_jars/ this_useful.jar
        

        您最终会在 JAR 的根目录中拥有 this_useful.jar:

        $ jar tf jar-file | grep this_useful.jar
        this_useful.jar
        

        【讨论】:

          【解决方案8】:

          如果有人需要以编程方式回答,这里就是。

          private static void createJar(File source, JarOutputStream target) {
              createJar(source, source, target);
          }
          
          private static void createJar(File source, File baseDir, JarOutputStream target) {
              BufferedInputStream in = null;
          
              try {
                  if (!source.exists()) {
                      throw new IOException("Source directory is empty");
                  }
                  if (source.isDirectory()) {
                      // For Jar entries, all path separates should be '/'(OS independent)
                      String name = source.getPath().replace("\\", "/");
                      if (!name.isEmpty()) {
                          if (!name.endsWith("/")) {
                              name += "/";
                          }
                          JarEntry entry = new JarEntry(name);
                          entry.setTime(source.lastModified());
                          target.putNextEntry(entry);
                          target.closeEntry();
                      }
                      for (File nestedFile: source.listFiles()) {
                          createJar(nestedFile, baseDir, target);
                      }
                      return;
                  }
          
                  String entryName = baseDir.toPath().relativize(source.toPath()).toFile().getPath().replace("\\", "/");
                  JarEntry entry = new JarEntry(entryName);
                  entry.setTime(source.lastModified());
                  target.putNextEntry(entry); in = new BufferedInputStream(new FileInputStream(source));
          
                  byte[] buffer = new byte[1024];
                  while (true) {
                      int count = in .read(buffer);
                      if (count == -1)
                          break;
                      target.write(buffer, 0, count);
                  }
                  target.closeEntry();
              } catch (Exception ignored) {
          
              } finally {
                  if ( in != null) {
                      try { in .close();
                      } catch (Exception ignored) {
                          throw new RuntimeException(ignored);
                      }
                  }
              }
          }
          

          【讨论】:

            【解决方案9】:
            String cmd = "jar uvf " + "jarName" + " " + "Filename";
            System.out.println(cmd);
            try {
            Process p = Runtime.getRuntime().exec(cmd);
            }
            catch (Exception ex) {
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2019-12-04
              • 1970-01-01
              • 2011-10-26
              • 2023-01-26
              • 2012-06-14
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多