java - Why it is impossible to delete entries from jar file? -


It is possible to add new entries in the jar file using jar tool.
It is possible to modify some entries in the Jar file using the Jar Tool.
It is impossible to delete some entries from the jar file. Why?

The intention of a jar file is to package a Java application

here There are three possible ways you can do that.

  1. You can use Winzip or some other zip utility
  2. You can use jars with just the right entries (delete jars and redirects).
  3. You can do this programmatically (sorted ... see below)

    Import java.io ; Import java.util ; Import java.util.zip ; Import java.util.jar ;

      Public class zerupdate {/ ** * main () * / public static zero main (string [] args) throws IOException {// received the name and entry name of the name from the command line Do string string = args [0]; String filename = args [1]; // Create a file descriptor for a jar and a temporary jar file jarfile = new file (jarnname); File tempJarFile = new file (jarName + ".tmp"); // Open the jar file. Jarfile jar = new jarfile (jarfile); System.out.println (pronounced + "opened."); // Start a flag indicating that the jar was updated. Boolean Jar Update = false; Try {// Create a temporary jar file with a manifest (copy of the manifest will be copied to // when the entries are copied.) JarManifest = jar.getManifest (); JarOutputStream tempJar = New JarOutputStream (new FileOutputStream (tempJarFile)); // Allocate a buffer to read the entry data. Byte [] buffer = new byte [1024]; Int bytes read; Try {// Open File In FileInputStream File = New FileInputStream (fileName); Try {// Create a jar entry and add it to the temporary jar. Gentry entry = new gallery (filename); TempJar.putNextEntry (login); // Read the file and write it in jar. While ((BytesRed = File Reid (buffer))! = -1) {tempJar.write (buffer, 0, bytes read); } System.out.println (entry.getName () + "added."); } Finally {file.close (); } // loop jar through entries and adding them to a temporary jar, // Skipping the entry that was already added to the temporary jar (Enumeration entries = jar.entries (); entries.hasMoreElements ();) {// Get the next entry Gentry Entry = (Zentri) entries. NXElment (); // If the entry has not already been added, add it. If (! Entry.getName (.) Equals (fileName)) {// Get input stream for entry InputStream entryStream = jar.getInputStream (entry); // Read the entry and write it in the temporary jar. TempJar.putNextEntry (login); While ((bytes read = entry stream.bdr)) = -1) {tempJar.write (buffer, 0, bytes read); }}} JarUpdated = true; } Hold (Exception Pre) {System.out.println (ex); // Add a stub entry here, so that the jar closes without an exception. TempJar.putNextEntry (new JarEntry ("stub"); } Finally {tempJar.close (); }} Finally {jar.close (); System.out.println (pronounced + "off."); // If the jar was not updated, remove the temporary jar file. If (! Jar updated) {tempJarFile.delete (); }} // If the jar was updated, delete the original jar file and rename the // temp jar file to the original name. If (jar updated) {jarFile.delete (); TempJarFile.renameTo (jarFile); System.out.println (pronounced + "updates."); }  

    }}


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -