Advertisement
Guest User

FatJarLauncher.java

a guest
May 30th, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. package com.ra4king.fatjar;
  2.  
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.InputStream;
  6. import java.util.Enumeration;
  7. import java.util.Random;
  8. import java.util.jar.JarEntry;
  9. import java.util.jar.JarFile;
  10.  
  11. import javax.swing.JOptionPane;
  12.  
  13. public class FatJarLauncher {
  14.     public static void main(String[] args) {
  15.         JarFile jar;
  16.         try {
  17.             jar = new JarFile(new File(FatJarLauncher.class.getProtectionDomain().getCodeSource().getLocation().toURI()));
  18.         }
  19.         catch(Exception exc) {
  20.             exc.printStackTrace();
  21.             JOptionPane.showMessageDialog(null, "Error while opening up jar file");
  22.             return;
  23.         }
  24.        
  25.         File file = new File(System.getProperty("java.io.tmpdir").concat("/natives").concat(String.valueOf(new Random().nextInt())).concat("/"));
  26.        
  27.         if(!file.exists())
  28.             file.mkdir();
  29.        
  30.         try {
  31.             for(Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements();) {
  32.                 JarEntry entry = e.nextElement();
  33.                
  34.                 if(entry.getName().contains("/"))
  35.                     continue;
  36.                
  37.                 if(isNative(entry.getName())) {
  38.                     try {
  39.                         FileOutputStream fos = new FileOutputStream(new File(file,entry.getName()));
  40.                        
  41.                         InputStream in = jar.getInputStream(entry);
  42.                        
  43.                         byte[] data = new byte[1024];
  44.                         int len;
  45.                         while((len = in.read(data)) != -1)
  46.                             fos.write(data,0,len);
  47.                        
  48.                         in.close();
  49.                         fos.close();
  50.                     }
  51.                     catch(Exception exc) {
  52.                         exc.printStackTrace();
  53.                         JOptionPane.showMessageDialog(null, "Error while extracting natives");
  54.                         return;
  55.                     }
  56.                 }
  57.             }
  58.            
  59.             try {
  60.                 Process p = Runtime.getRuntime().exec("java -Djava.library.path=\"" + file.getAbsolutePath() + "\" -cp \"" + jar.getName() + "\" " + jar.getManifest().getMainAttributes().getValue("Launcher-Main-Class"));
  61.                
  62.                 p.waitFor();
  63.             }
  64.             catch(Exception exc) {
  65.                 exc.printStackTrace();
  66.                 JOptionPane.showMessageDialog(null, "Error while launching application.");
  67.             }
  68.         }
  69.         finally {
  70.             deleteDir(file);
  71.         }
  72.     }
  73.    
  74.     private static boolean isNative(String path) {
  75.         path = path.toLowerCase();
  76.        
  77.         String os = System.getProperty("os.name").toLowerCase();
  78.        
  79.         if(os.startsWith("win"))
  80.             return path.endsWith(".dll");
  81.        
  82.         if(os.startsWith("linux"))
  83.             return path.endsWith(".so");
  84.        
  85.         if(os.startsWith("mac") || os.startsWith("darwin"))
  86.             return path.endsWith(".dylib") || path.endsWith(".jnilib");
  87.        
  88.         return false;
  89.     }
  90.    
  91.     private static void deleteDir(File file) {
  92.         for(File f : file.listFiles()) {
  93.             if(f.isDirectory())
  94.                 deleteDir(f);
  95.            
  96.             f.delete();
  97.         }
  98.        
  99.         file.delete();
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement