Ver Mensaje Individual
  #3 (permalink)  
Antiguo 24/02/2009, 16:51
oscarmoal
 
Fecha de Ingreso: marzo-2008
Mensajes: 63
Antigüedad: 16 años, 10 meses
Puntos: 0
Respuesta: arrancar una jar desde otro

encontre este código me parece que es muy util, se los dejo para que la revisen,

Código:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.jar.*;

/**
 * This utility makes a JAR runnable by inserting a <code>Main-Class</code>
 * attribute into the Manifest.
 *
 * @author Shawn Silverman
 */
public class MakeJarRunnable {
    public static void main(String[] args) throws IOException {
        if (args.length != 2) {
            System.out.println("Usage: MakeJarRunnable "
                               + "<jar file> <Main-Class>");
            System.exit(0);
        }

        // Create the JarInputStream object, and get its Manifest

        JarInputStream jarIn = new JarInputStream(new FileInputStream(args[0]));
        Manifest manifest = jarIn.getManifest();
        if (manifest == null) {
            // This will happen if there is no Manifest

            manifest = new Manifest();
        }

        Attributes a = manifest.getMainAttributes();
        String oldMainClass = a.putValue("Main-Class", args[1]);

        // If there was an old value there, tell the user about it and exit

        if (oldMainClass != null) {
            System.out.println("Warning: old Main-Class value is: "
                               + oldMainClass);
            System.exit(1);
        }

        System.out.println("Writing to " + args[2] + "...");
        JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(args[2]),
                                                     manifest);

        // Create a read buffer to be used for transferring data from the input

        byte[] buf = new byte[4096];

        // Iterate the entries

        JarEntry entry;
        while ((entry = jarIn.getNextJarEntry()) != null) {
            // Exclude the Manifest file from the old JAR

            if ("META-INF/MANIFEST.MF".equals(entry.getName())) continue;

            // Write out the entry to the output JAR

            jarOut.putNextEntry(entry);
            int read;
            while ((read = jarIn.read(buf)) != -1) {
                jarOut.write(buf, 0, read);
            }

            jarOut.closeEntry();
        }

        // Flush and close all the streams

        jarOut.flush();
        jarOut.close();

        jarIn.close();
    }
}
Saludos