我想将以前从其他文件中提取的一系列文件(已经完成)添加到 jar 中。这些文件将覆盖 JAR 中的文件。最有效的方法是什么?我需要它快。谢谢!
			
			53468 次
		
9 回答
            21        
        
		
jar -u file.jar file1 file2 file3 ...
于 2012-09-02T21:09:07.950   回答
    
    
            20        
        
		
jar -uf my.jar file1 file2...
jar -uf my.jar dir/
或混合
jar -uf my.jar file dir/
于 2017-06-23T16:08:35.030   回答
    
    
            10        
        
		
JAR 文件是一个 ZIP 文件,请记住。
只需使用一些 ZIP 库。
于 2012-09-02T21:10:46.870   回答
    
    
            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
解决方案是使用0jar 选项:
jar uvf0 myfile.jar BOOT-INF/lib/file.jar
对于普通的类文件,您不需要它。
于 2020-04-03T09:51:24.293   回答
    
    
            3        
        
		
 zip file.jar file1 file2 file3 
在 Mac Os 10.7.5 中为我工作
于 2013-04-12T21:42:02.730   回答
    
    
            2        
        
		
//Add a file in jar in a particular folder
jar uvf <jar file name> <file to be added> <folder name inside jar>
于 2019-07-31T07:30:03.347   回答
    
    
            1        
        
		
如果有人需要以编程方式回答,就在这里。
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);
            }
        }
    }
}
于 2019-12-16T06:56:45.887   回答
    
    
            1        
        
		
扩展现有答案,我发现jar选项在添加位于它们自己的文件夹中的文件并且您将它们的路径展平时非常有用。-C 
$ 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
于 2018-10-22T14:55:59.657   回答
    
    
            -1        
        
		
String cmd = "jar uvf " + "jarName" + " " + "Filename";
System.out.println(cmd);
try {
Process p = Runtime.getRuntime().exec(cmd);
}
catch (Exception ex) {
}
于 2019-07-31T06:16:26.443   回答