Advertisement
Guest User

FatJar.java

a guest
May 30th, 2012
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.50 KB | None | 0 0
  1. package com.ra4king.fatjar;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.util.ArrayList;
  9. import java.util.Enumeration;
  10. import java.util.jar.Attributes;
  11. import java.util.jar.JarEntry;
  12. import java.util.jar.JarFile;
  13. import java.util.jar.JarOutputStream;
  14. import java.util.jar.Manifest;
  15.  
  16. public class FatJar {
  17.     public static void main(String[] args) {
  18.         System.out.println("FatJar 1.0 by Roi Atalla (ra4king)");
  19.        
  20.         if(args.length != 8) {
  21.             printUsage();
  22.             return;
  23.         }
  24.        
  25.         String libs[] = null, natives[] = null, main = null, output = null;
  26.        
  27.         for(int a = 0; a < 8; a++) {
  28.             String s = args[a];
  29.            
  30.             if(s.length() == 0 || s.charAt(0) != '-') {
  31.                 printUsage();
  32.                 return;
  33.             }
  34.            
  35.             s = s.toLowerCase();
  36.            
  37.             if(s.equalsIgnoreCase("-libs"))
  38.                 libs = args[++a].split(";");
  39.             else if(s.equalsIgnoreCase("-natives"))
  40.                 natives = args[++a].split(";");
  41.             else if(s.equalsIgnoreCase("-main"))
  42.                 main = args[++a];
  43.             else if(s.equalsIgnoreCase("-output"))
  44.                 output = args[++a];
  45.         }
  46.        
  47.         if(libs == null || natives == null || main == null || output == null) {
  48.             printUsage();
  49.             return;
  50.         }
  51.        
  52.         Manifest manifest = new Manifest();
  53.         Attributes attrib = manifest.getMainAttributes();
  54.         attrib.putValue("Manifest-Version", "1.0");
  55.         attrib.putValue("Main-Class", "com.ra4king.fatjar.FatJarLauncher");
  56.         attrib.putValue("Launcher-Main-Class", main);
  57.        
  58.         File outputFile;
  59.         JarOutputStream jarOS;
  60.         try {
  61.             outputFile= new File(output);
  62.             outputFile.createNewFile();
  63.            
  64.             jarOS = new JarOutputStream(new FileOutputStream(outputFile), manifest);
  65.         }
  66.         catch(Exception exc) {
  67.             System.out.println("Output file is invalid.");
  68.             exc.printStackTrace();
  69.             return;
  70.         }
  71.        
  72.         try {
  73.             ArrayList<String> dirs = new ArrayList<String>();
  74.            
  75.             for(String lib : libs) {
  76.                 File file = new File(lib);
  77.                
  78.                 if(!file.exists())
  79.                     throw new IllegalArgumentException("Lib does not exist: " + lib);
  80.                
  81.                 try {
  82.                     JarFile jar = new JarFile(file);   
  83.                     for(Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements();) {
  84.                         JarEntry entry = e.nextElement();
  85.                        
  86.                         if(dirs.contains(entry.getName()))
  87.                             continue;
  88.                         else
  89.                             dirs.add(entry.getName());
  90.                        
  91.                         if(entry.getName().toLowerCase().startsWith("meta-inf"))
  92.                             continue;
  93.                        
  94.                         InputStream in = jar.getInputStream(entry);
  95.                        
  96.                         jarOS.putNextEntry(new JarEntry(entry.getName()));
  97.                        
  98.                         byte[] data = new byte[1024];
  99.                         int len;
  100.                         while((len = in.read(data)) != -1)
  101.                             jarOS.write(data,0,len);
  102.                        
  103.                         in.close();
  104.                         jarOS.closeEntry();
  105.                     }
  106.                     jar.close();
  107.                 }
  108.                 catch(IOException exc) {
  109.                     throw new IllegalArgumentException("Problem with lib: " + lib,exc);
  110.                 }
  111.             }
  112.            
  113.             for(String n : natives) {
  114.                 File file = new File(n);
  115.                
  116.                 if(!file.exists())
  117.                     throw new IllegalArgumentException("Native does not exist: " + n);
  118.                
  119.                 FileInputStream fis = new FileInputStream(file);
  120.                
  121.                 jarOS.putNextEntry(new JarEntry(getFileName(n)));
  122.                
  123.                 byte[] data = new byte[1024];
  124.                 int len;
  125.                 while((len = fis.read(data)) != -1)
  126.                     jarOS.write(data, 0, len);
  127.                
  128.                 fis.close();
  129.                 jarOS.closeEntry();
  130.             }
  131.            
  132.             InputStream in = FatJar.class.getResourceAsStream("FatJarLauncher.class");
  133.             jarOS.putNextEntry(new JarEntry("com/ra4king/fatjar/FatJarLauncher.class"));
  134.            
  135.             byte[] data = new byte[1024];
  136.             int len;
  137.             while((len = in.read(data)) != -1)
  138.                 jarOS.write(data,0,len);
  139.            
  140.             in.close();
  141.             jarOS.closeEntry();
  142.            
  143.             jarOS.close();
  144.         }
  145.         catch(Exception exc) {
  146.             if(exc.getCause() == null)
  147.                 System.err.println(exc.getMessage());
  148.             else
  149.                 exc.printStackTrace();
  150.            
  151.             try {
  152.                 jarOS.close();
  153.             }
  154.             catch(Exception exc2) {}
  155.            
  156.             outputFile.delete();
  157.         }
  158.     }
  159.    
  160.     private static void printUsage() {
  161.         System.err.println("Usage: -libs [libs] -natives [natives] -main [main class] -output [path]");
  162.         System.err.println("Both [libs] and [natives] can be a semi-colon-separated list of files, relative or absolute");
  163.         System.err.println("The [main class] is in this format: com.example.MainExample");
  164.         System.err.println("The output [path] is the path, relative or absolute, of the final Fat Jar");
  165.     }
  166.    
  167.     private static String getFileName(String path) {
  168.         path = path.replace('\\', '/');
  169.         return path.substring(path.indexOf('/') + 1);
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement