import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
public class CrearJarEjecutable {
/**
* Uso:
* java CrearJarEjecutable myjar.jar ClasePrincipal myjar_r.jar
*
*/
public static void main(String[] args) {
if (args.length != 3) {
System.out.println("Uso: CrearJarEjecutable " + "reproductor skin <output>");
System.exit(0);
}
JarInputStream jarIn = null;
//Creamos el objeto JarInputStream, y obtenemos su manifiest
try {
jarIn = new JarInputStream(new FileInputStream(args[0]));
Manifest manifest = jarIn.getManifest();
if (manifest == null) {
manifest = new Manifest();
}
Attributes a = manifest.getMainAttributes();
String oldMainClass = a.putValue("Main-Class", args[1]);
//Si ya existe un manifiest, notificamos y salimos
if (oldMainClass != null) {
System.out.println("Cuidado: ya existe un valor Main-Class: " + oldMainClass);
System.exit(1);
}
System.out.println("Writing to " + args[2] + "...");
JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(args[2]), manifest);
//Creamos un buffer de lectura para transferir los datos desde el jar original
byte[] buf = new byte[4096];
//Iteramos las entradas
JarEntry entry;
while ((entry = jarIn.getNextJarEntry()) != null) {
//Excluimos el manifiest del viejo jar
if ("META-INF/MANIFEST.MF".equals(entry.getName())) continue;
//Escribimos la entrada en el nuevo jar
jarOut.putNextEntry(entry);
int read;
while ((read = jarIn.read(buf)) != -1) {
jarOut.write(buf, 0, read);
}
jarOut.closeEntry();
}
//Cerramos los streams
jarOut.flush();
jarOut.close();
jarIn.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}