} jos.closeEntry(); } jos.flush(); }最后{ 试一试{ jos.close(); }捕获(例外e){ } } } public void unjar()引发IOException{ JarFile JarFile=null; 试一试{ 字符串outputPath=输出路径; File outputPathFile=新文件(outputPath); //生成输出目录。 //我将让您决定如何最好地处理现有内容;) outputPathFile.mkdirs(); //创建新的JAR文件引用 jarFile=newjarfile(新文件(“C:/hold/Java_Harmony.jar”); //获取所有条目的列表 枚举条目=jarFile.entries(); while(entries.hasMoreElements()){ //获取下一个条目 JarEntry=entries.nextElement(); //引用文件 文件路径=新文件(outputPath+File.separator+entry.getName()); if(entry.isDirectory()){ //如果可以,创建目录结构 如果(!path.exists()&&!path.mkdirs()){ 抛出新IOException(“未能创建输出路径”+路径); } }否则{ System.out.println(“提取”+路径); //从Jar中提取文件并将其写入磁盘 InputStream=null; OutputStream os=null; 试一试{ is=jarFile.getInputStream(条目); os=新文件输出流(路径); byte[]byteBuffer=新字节[1024]; int字节读取=-1; 而((bytesRead=is.read(byteBuffer))!=-1){ 写操作(字节缓冲,0,字节读取); } os.flush(); }最后{ 试一试{ os.close(); }捕获(例外e){ } 试一试{ is.close(); }捕获(例外e){ } } } } }最后{ 试一试{ jarFile.close(); }捕获(例外e){ } } } }

} jos.closeEntry(); } jos.flush(); }最后{ 试一试{ jos.close(); }捕获(例外e){ } } } public void unjar()引发IOException{ JarFile JarFile=null; 试一试{ 字符串outputPath=输出路径; File outputPathFile=新文件(outputPath); //生成输出目录。 //我将让您决定如何最好地处理现有内容;) outputPathFile.mkdirs(); //创建新的JAR文件引用 jarFile=newjarfile(新文件(“C:/hold/Java_Harmony.jar”); //获取所有条目的列表 枚举条目=jarFile.entries(); while(entries.hasMoreElements()){ //获取下一个条目 JarEntry=entries.nextElement(); //引用文件 文件路径=新文件(outputPath+File.separator+entry.getName()); if(entry.isDirectory()){ //如果可以,创建目录结构 如果(!path.exists()&&!path.mkdirs()){ 抛出新IOException(“未能创建输出路径”+路径); } }否则{ System.out.println(“提取”+路径); //从Jar中提取文件并将其写入磁盘 InputStream=null; OutputStream os=null; 试一试{ is=jarFile.getInputStream(条目); os=新文件输出流(路径); byte[]byteBuffer=新字节[1024]; int字节读取=-1; 而((bytesRead=is.read(byteBuffer))!=-1){ 写操作(字节缓冲,0,字节读取); } os.flush(); }最后{ 试一试{ os.close(); }捕获(例外e){ } 试一试{ is.close(); }捕获(例外e){ } } } } }最后{ 试一试{ jarFile.close(); }捕获(例外e){ } } } },java,installation,minecraft,Java,Installation,Minecraft,您可以使用这个非常简单的库来打包/解包jar文件 public class JarTest { protected static final String OUTPUT_PATH = "..."; // The place you want to extact the jar to /** * @param args the command line arguments */ public static void main(String[] arg

您可以使用这个非常简单的库来打包/解包jar文件

public class JarTest {

    protected static final String OUTPUT_PATH = "..."; // The place you want to extact the jar to

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        new JarTest();

    }

    public JarTest() {

        try {

            unjar();
            // Copy new contents in...
            jar();

        } catch (IOException exp) {

            exp.printStackTrace();

        }

    }

    // This just recursivly lists through all the files to be included in the new jar
    // We don't care about the directories, as we will create them from the file
    // references in the Jar ourselves
    protected List<File> getFiles(File path) {

        List<File> lstFiles = new ArrayList<File>(25);

        // If you want the directories, add the "path" to the list now...

        File[] files = path.listFiles();
        if (files != null && files.length > 0) {

            for (File file : files) {

                if (file.isDirectory()) {

                    lstFiles.addAll(getFiles(file));

                } else {

                    lstFiles.add(file);

                }

            }

        }


        return lstFiles;

    }

    // Re-Jar the contents
    // You should always attempt to jar back to a new file, as you may not want to effect the original ;)
    public void jar() throws IOException {

        JarOutputStream jos = null;

        try {

            String outputPath = OUTPUT_PATH;

            // Create a new JarOutputStream to the file you want to create
            jos = new JarOutputStream(new FileOutputStream("...")); // Add your file reference

            List<File> fileList = getFiles(new File(OUTPUT_PATH));
            System.out.println("Jaring " + fileList.size() + " files");

            // Okay, I cheat.  I make a list of all the paths already added to the Jar only create
            // them when I need to.  You could use "file.isDirectory", but that would mean you would need
            // to ensure that the files were sorted to allow all the directories to be first
            // or make sure that the directory reference is added to the start of each recursion list
            List<String> lstPaths = new ArrayList<String>(25);
            for (File file : fileList) {

                // Replace the Windows file seperator
                // We only want the path to this element
                String path = file.getParent().replace("\\", "/");
                // Get the name of the file
                String name = file.getName();

                // Remove the output path from the start of the path
                path = path.substring(outputPath.length());
                // Remove the leading slash if it exists
                if (path.startsWith("/")) {

                    path = path.substring(1);

                }

                // Add the path path reference to the Jar
                // A JarEntry is considered to be a directory if it ends with "/"
                if (path.length() > 0) {

                    // At the trailing path seperator
                    path += "/";

                    // Check to see if we've already added it out not
                    if (!lstPaths.contains(path)) {

                        // At the path entry...we need need this to make it easier to 
                        // extract the files at a later state. There is a way to cheat,
                        // but I'll let you figure it out
                        JarEntry entry = new JarEntry(path);
                        jos.putNextEntry(entry);
                        jos.closeEntry();

                        // Make sure we don't try to add the same path entry again
                        lstPaths.add(path);

                    }

                }

                System.out.println("Adding " + path + name);

                // Create the actual entry for this file
                JarEntry entry = new JarEntry(path + name);
                jos.putNextEntry(entry);

                // Write the entry to the file
                FileInputStream fis = null;
                try {

                    fis = new FileInputStream(file);
                    byte[] byteBuffer = new byte[1024];
                    int bytesRead = -1;
                    while ((bytesRead = fis.read(byteBuffer)) != -1) {

                        jos.write(byteBuffer, 0, bytesRead);

                    }

                    jos.flush();

                } finally {

                    try {
                        fis.close();
                    } catch (Exception e) {
                    }

                }

                jos.closeEntry();

            }

            jos.flush();

        } finally {

            try {
                jos.close();
            } catch (Exception e) {
            }

        }

    }

    public void unjar() throws IOException {

        JarFile jarFile = null;

        try {

            String outputPath = OUTPUT_PATH;
            File outputPathFile = new File(outputPath);
            // Make the output directories.
            // I'll leave it up to you to decide how best to deal with existing content ;)
            outputPathFile.mkdirs();

            // Create a new JarFile reference
            jarFile = new JarFile(new File("C:/hold/Java_Harmony.jar"));

            // Get a list of all the entries
            Enumeration<JarEntry> entries = jarFile.entries();
            while (entries.hasMoreElements()) {

                // Get the next entry
                JarEntry entry = entries.nextElement();
                // Make a file reference
                File path = new File(outputPath + File.separator + entry.getName());
                if (entry.isDirectory()) {

                    // Make the directory structure if we can
                    if (!path.exists() && !path.mkdirs()) {

                        throw new IOException("Failed to create output path " + path);

                    }

                } else {

                    System.out.println("Extracting " + path);

                    // Extract the file from the Jar and write it to disk
                    InputStream is = null;
                    OutputStream os = null;
                    try {

                        is = jarFile.getInputStream(entry);
                        os = new FileOutputStream(path);

                        byte[] byteBuffer = new byte[1024];
                        int bytesRead = -1;
                        while ((bytesRead = is.read(byteBuffer)) != -1) {

                            os.write(byteBuffer, 0, bytesRead);

                        }

                        os.flush();

                    } finally {

                        try {
                            os.close();
                        } catch (Exception e) {
                        }

                        try {
                            is.close();
                        } catch (Exception e) {
                        }

                    }

                }

            }

        } finally {

            try {
                jarFile.close();
            } catch (Exception e) {
            }

        }

    }
}

很简单

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import fr.stevecohen.jarmanager.JarPacker;
import fr.stevecohen.jarmanager.JarUnpacker;

public class MyClass {

    public void addFileToJar(String jarPath, String otherFilePath) {
        try {
            JarUnpacker jarUnpacker = new JarUnpacker();
            File myJar = new File("./myfile.jar");
            File otherFile = new File(otherFilePath);

            Path unpackDir = Files.createTempDirectory(myJar.getName()); //create a temp directory to extract your jar
            System.out.println("Unpacking in " + unpackDir.toString());
            jarUnpacker.unpack(jarPath, unpackDir.toString()); //extraxt all files contained in the jar in temp directory

            Files.copy(otherFile.toPath(), new File(unpackDir.toFile(), otherFile.getName()).toPath()); //copy your file

            JarPacker jarRepacker = new JarPacker();
            File newJar = new File("./maNewFile.jar");
            System.out.println("Packing jar in " + newJar.getAbsolutePath());
            jarRepacker.pack(unpackDir.toString(), newJar.getAbsolutePath()); //repack the jar with the new files inside
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
您还可以使用maven依赖关系

<dependency>
    <groupId>fr.stevecohen.jarmanager</groupId>
    <artifactId>JarManager</artifactId>
    <version>0.5.0</version>
</dependency>

史蒂文·科恩神父

如果您发现一些bug

我希望您无法在为应用程序加载的jar文件中更改这些bug。为什么不尝试提取和复制这些文件,因为这应该很简单。@JamesBlack任何能让我用我想要的文件重新打包一个.jar的东西都是调用“jar”命令的最简单的方法,如果你正在放置-u,我想-a可能是用来添加的。谢谢,这看起来很有用,但我不在Java7上。我使用Java6,我没有时间重新学习一些语法6@The_Steve13现在举个例子
<dependency>
    <groupId>fr.stevecohen.jarmanager</groupId>
    <artifactId>JarManager</artifactId>
    <version>0.5.0</version>
</dependency>
<repository>
    <id>repo-reapersoon</id>
    <name>ReaperSoon's repo</name>
    <url>http://repo-maven.stevecohen.fr</url>
</repository>