Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ra4king.fatjar;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.util.Enumeration;
- import java.util.Random;
- import java.util.jar.JarEntry;
- import java.util.jar.JarFile;
- import javax.swing.JOptionPane;
- public class FatJarLauncher {
- public static void main(String[] args) {
- JarFile jar;
- try {
- jar = new JarFile(new File(FatJarLauncher.class.getProtectionDomain().getCodeSource().getLocation().toURI()));
- }
- catch(Exception exc) {
- exc.printStackTrace();
- JOptionPane.showMessageDialog(null, "Error while opening up jar file");
- return;
- }
- File file = new File(System.getProperty("java.io.tmpdir").concat("/natives").concat(String.valueOf(new Random().nextInt())).concat("/"));
- if(!file.exists())
- file.mkdir();
- try {
- for(Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements();) {
- JarEntry entry = e.nextElement();
- if(entry.getName().contains("/"))
- continue;
- if(isNative(entry.getName())) {
- try {
- FileOutputStream fos = new FileOutputStream(new File(file,entry.getName()));
- InputStream in = jar.getInputStream(entry);
- byte[] data = new byte[1024];
- int len;
- while((len = in.read(data)) != -1)
- fos.write(data,0,len);
- in.close();
- fos.close();
- }
- catch(Exception exc) {
- exc.printStackTrace();
- JOptionPane.showMessageDialog(null, "Error while extracting natives");
- return;
- }
- }
- }
- try {
- Process p = Runtime.getRuntime().exec("java -Djava.library.path=\"" + file.getAbsolutePath() + "\" -cp \"" + jar.getName() + "\" " + jar.getManifest().getMainAttributes().getValue("Launcher-Main-Class"));
- p.waitFor();
- }
- catch(Exception exc) {
- exc.printStackTrace();
- JOptionPane.showMessageDialog(null, "Error while launching application.");
- }
- }
- finally {
- deleteDir(file);
- }
- }
- private static boolean isNative(String path) {
- path = path.toLowerCase();
- String os = System.getProperty("os.name").toLowerCase();
- if(os.startsWith("win"))
- return path.endsWith(".dll");
- if(os.startsWith("linux"))
- return path.endsWith(".so");
- if(os.startsWith("mac") || os.startsWith("darwin"))
- return path.endsWith(".dylib") || path.endsWith(".jnilib");
- return false;
- }
- private static void deleteDir(File file) {
- for(File f : file.listFiles()) {
- if(f.isDirectory())
- deleteDir(f);
- f.delete();
- }
- file.delete();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement