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.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.Enumeration;
- import java.util.jar.Attributes;
- import java.util.jar.JarEntry;
- import java.util.jar.JarFile;
- import java.util.jar.JarOutputStream;
- import java.util.jar.Manifest;
- public class FatJar {
- public static void main(String[] args) {
- System.out.println("FatJar 1.0 by Roi Atalla (ra4king)");
- if(args.length != 8) {
- printUsage();
- return;
- }
- String libs[] = null, natives[] = null, main = null, output = null;
- for(int a = 0; a < 8; a++) {
- String s = args[a];
- if(s.length() == 0 || s.charAt(0) != '-') {
- printUsage();
- return;
- }
- s = s.toLowerCase();
- if(s.equalsIgnoreCase("-libs"))
- libs = args[++a].split(";");
- else if(s.equalsIgnoreCase("-natives"))
- natives = args[++a].split(";");
- else if(s.equalsIgnoreCase("-main"))
- main = args[++a];
- else if(s.equalsIgnoreCase("-output"))
- output = args[++a];
- }
- if(libs == null || natives == null || main == null || output == null) {
- printUsage();
- return;
- }
- Manifest manifest = new Manifest();
- Attributes attrib = manifest.getMainAttributes();
- attrib.putValue("Manifest-Version", "1.0");
- attrib.putValue("Main-Class", "com.ra4king.fatjar.FatJarLauncher");
- attrib.putValue("Launcher-Main-Class", main);
- File outputFile;
- JarOutputStream jarOS;
- try {
- outputFile= new File(output);
- outputFile.createNewFile();
- jarOS = new JarOutputStream(new FileOutputStream(outputFile), manifest);
- }
- catch(Exception exc) {
- System.out.println("Output file is invalid.");
- exc.printStackTrace();
- return;
- }
- try {
- ArrayList<String> dirs = new ArrayList<String>();
- for(String lib : libs) {
- File file = new File(lib);
- if(!file.exists())
- throw new IllegalArgumentException("Lib does not exist: " + lib);
- try {
- JarFile jar = new JarFile(file);
- for(Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements();) {
- JarEntry entry = e.nextElement();
- if(dirs.contains(entry.getName()))
- continue;
- else
- dirs.add(entry.getName());
- if(entry.getName().toLowerCase().startsWith("meta-inf"))
- continue;
- InputStream in = jar.getInputStream(entry);
- jarOS.putNextEntry(new JarEntry(entry.getName()));
- byte[] data = new byte[1024];
- int len;
- while((len = in.read(data)) != -1)
- jarOS.write(data,0,len);
- in.close();
- jarOS.closeEntry();
- }
- jar.close();
- }
- catch(IOException exc) {
- throw new IllegalArgumentException("Problem with lib: " + lib,exc);
- }
- }
- for(String n : natives) {
- File file = new File(n);
- if(!file.exists())
- throw new IllegalArgumentException("Native does not exist: " + n);
- FileInputStream fis = new FileInputStream(file);
- jarOS.putNextEntry(new JarEntry(getFileName(n)));
- byte[] data = new byte[1024];
- int len;
- while((len = fis.read(data)) != -1)
- jarOS.write(data, 0, len);
- fis.close();
- jarOS.closeEntry();
- }
- InputStream in = FatJar.class.getResourceAsStream("FatJarLauncher.class");
- jarOS.putNextEntry(new JarEntry("com/ra4king/fatjar/FatJarLauncher.class"));
- byte[] data = new byte[1024];
- int len;
- while((len = in.read(data)) != -1)
- jarOS.write(data,0,len);
- in.close();
- jarOS.closeEntry();
- jarOS.close();
- }
- catch(Exception exc) {
- if(exc.getCause() == null)
- System.err.println(exc.getMessage());
- else
- exc.printStackTrace();
- try {
- jarOS.close();
- }
- catch(Exception exc2) {}
- outputFile.delete();
- }
- }
- private static void printUsage() {
- System.err.println("Usage: -libs [libs] -natives [natives] -main [main class] -output [path]");
- System.err.println("Both [libs] and [natives] can be a semi-colon-separated list of files, relative or absolute");
- System.err.println("The [main class] is in this format: com.example.MainExample");
- System.err.println("The output [path] is the path, relative or absolute, of the final Fat Jar");
- }
- private static String getFileName(String path) {
- path = path.replace('\\', '/');
- return path.substring(path.indexOf('/') + 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement