Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you make a function that will create a .jar file in Java?
    primarykey
    data
    text
    <p>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. 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 <code>diff -rqy</code> on them both, it didn't show any difference.</p> <p>Here is the current function:</p> <pre><code>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(); } } </code></pre> <p>I call it like this:</p> <pre><code>JarOutputStream zip = new JarOutputStream( new FileOutputStream(JARFILE)); for (File nestedFile : new File(DIRECTORY).listFiles()) { Utils.add(nestedFile, zip, new File(DIRECTORY).getAbsolutePath()); } zip.close(); </code></pre> <p>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.</p> <p>Thanks in advance!</p> <p>Edit: I don't want something using the <code>jar</code> command, because I don't want the user to need to install the JDK. I want something using pure Java (libraries are OK, as long as I can include them in the program).</p> <p>Edit 2: I'm making a Minecraft modder (like MCPatcher and ModLoader), but when I run <code>java -jar minecraft.jar</code>, it gives me this: <code>Invalid or corrupt jarfile</code>. The correct .jar doesn't give this (just a main class error, which is supposed to happen).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload