简体   繁体   English

您如何制作将在Java中创建.jar文件的函数?

[英]How do you make a function that will create a .jar file in Java?

I've made some code in Java that will change some files in another .jar file, and I know that the unpacking/changing works, but the repacking doesn't. 我已经用Java编写了一些代码,这些代码将更改另一个.jar文件中的某些文件,而且我知道拆包/更改有效,但重新打包却无效。 It does succeed, but when I compare the new one and the original (I removed the code that changed the files), they differed. 它确实可以成功,但是当我比较新文件和原始文件时(我删除了更改文件的代码),它们有所不同。 What's interesting is that when I extracted them both into different directories, and I runned diff -rqy on them both, it didn't show any difference. 有趣的是,当我将它们都提取到不同的目录中并且对它们都运行diff -rqy时,它没有显示任何区别。

Here is the current function: 这是当前功能:

public static void add(File source, JarOutputStream target, String removeme)
        throws IOException
{
    BufferedInputStream in = null;
    try
    {
        File source2 = new File(source.getPath().replaceAll("^" + removeme,
                ""));
        // File source2 = source;
        if (source.isDirectory())
        {
            String name = source2.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())
                add(nestedFile, target, removeme);
            return;
        }

        JarEntry entry = new JarEntry(source2.getPath().replace("\\", "/"));
        entry.setTime(source.lastModified());
        target.putNextEntry(entry);
        in = new BufferedInputStream(new FileInputStream(source));

        byte[] buffer = new byte[2048];
        while (true)
        {
            int count = in.read(buffer);
            if (count == -1)
                break;
            target.write(buffer, 0, count);
        }
        target.closeEntry();
    }
    finally
    {
        if (in != null)
            in.close();
    }
}

I call it like this: 我这样称呼它:

JarOutputStream zip = new JarOutputStream(
                        new FileOutputStream(JARFILE));
                for (File nestedFile : new File(DIRECTORY).listFiles())
                {
                    Utils.add(nestedFile, zip,
                            new File(DIRECTORY).getAbsolutePath());
                }
                zip.close();

Can anyone direct me on what to change in the function, or what other function I should use? 谁能指导我更改功能或应使用的其他功能? The directory has subdirectories, so I need a function that will scan them. 该目录具有子目录,因此我需要一个可以对其进行扫描的功能。

Thanks in advance! 提前致谢!

Edit: I don't want something using the jar command, because I don't want the user to need to install the JDK. 编辑:我不想要使用jar命令的东西,因为我不想用户需要安装JDK。 I want something using pure Java (libraries are OK, as long as I can include them in the program). 我想要使​​用纯Java的东西(库可以,只要我可以将它们包含在程序中即可)。

Edit 2: I'm making a Minecraft modder (like MCPatcher and ModLoader), but when I run java -jar minecraft.jar , it gives me this: Invalid or corrupt jarfile . 编辑2:我正在制作Minecraft修改器(如MCPatcher和ModLoader),但是当我运行java -jar minecraft.jar ,它给了我这个: Invalid or corrupt jarfile The correct .jar doesn't give this (just a main class error, which is supposed to happen). 正确的.jar不会给出此信息(只是应该发生的主类错误)。

I think you maybe interested in java.util.jar. 我认为您可能对java.util.jar感兴趣。 This link maybe useful for you.. 该链接可能对您有用。

http://www.theserverside.com/discussions/thread.tss?thread_id=32600 http://www.theserverside.com/discussions/thread.tss?thread_id=32600

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM