1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package org.ninjacave.jarsplice;
import java.io.File;
import java.util.ArrayList;
import org.ninjacave.jarsplice.gui.JarSpliceFrame;
import org.ninjacave.jarsplice.core.*;

public class JarSplice
{
    private static String[] nativeTypes = new String[]{".dll",".so",".jnlib",".dylib"};
    private static String[] jarTypes = new String[]{".jar"};
    private static String macIconType = ".icns";
    public static void main(String[] args)
    {
        if(args.length == 0)
        {
            JarSpliceFrame gui = new JarSpliceFrame();
        }
        else
        {
            boolean fat=false, linux=false, mac=false, macicon=false, windows=false;//output bools
            String fatP="", linuxP="", macP="", maciconP="", macnameP="", windowsP="";
            boolean jars=false, natives=false, main=false, jvmArgs=false;//input bools
            String[] jarsP=new String[0], nativesP=new String[0];
            String mainP="", jvmArgsP="";
            
            int i=0;
            try{
                for(i=i; i<args.length; i++)
                {//Check output params
                    if(args[i].toLowerCase().equals("-fat")||args[i].toLowerCase().equals("-jar"))
                    {
                        if(fat)
                            printUsage(".jar output defined multiply");
                        fat=true;
                        i++;
                        fatP = args[i];
                    }
                    else if(args[i].toLowerCase().equals("-lin")||args[i].toLowerCase().equals("-linux")||args[i].toLowerCase().equals("-sh"))
                    {
                        if(linux)
                            printUsage(".sh output defined multiply");
                        linux=true;
                        i++;
                        linuxP = args[i];
                    }
                    else if(args[i].toLowerCase().equals("-app")||args[i].toLowerCase().equals("-mac"))
                    {
                        if(mac)
                            printUsage(".app output defined multiply");
                        mac=true;
                        i++;
                        macP = args[i];
                        i++;
                        macnameP = args[i];
                        if(args[i+1].toLowerCase().equals("-icon"))
                        {
                            i+=2;
                            if(args[i].toLowerCase().endsWith(macIconType))
                            {
                                maciconP = args[i];
                            }
                            else
                            {
                                System.out.println("The mac icon file "+args[i]+" is not of a .icns filetype, ignoring.");
                            }
                        }
                    }
                    else if(args[i].toLowerCase().equals("-win")||args[i].toLowerCase().equals("-windows")||args[i].toLowerCase().equals("-exe"))
                    {
                        if(windows)
                            printUsage(".exe output defined multiply");
                        windows=true;
                        i++;
                        windowsP = args[i];
                    }
                    else
                    {
                        break;
                    }
                }
            }catch(java.lang.ArrayIndexOutOfBoundsException e){printUsage("Expected parameter missing");};
            if(!(fat||linux||mac||windows))
            {//User hasnt specified an ouput
                printUsage("Atleast 1 output parameter must be specified");
            }
            else if(args.length==i)
            {
                printUsage("Input parameters required");
            }
            try{
                for(i=i; i<args.length; i++)
                {
                    if(args[i].toLowerCase().equals("-jars"))
                    {
                        if(jars)
                            printUsage(".jar's input defined multiply");
                        jars=true;
                        i++;
                        jarsP = args[i].split(";");
                    }
                    else if(args[i].toLowerCase().equals("-nats")||args[i].toLowerCase().equals("-natives"))
                    {
                        if(natives)
                            printUsage("natives input defined multiply");
                        natives=true;
                        i++;
                        nativesP = args[i].split(";");
                    }
                    else if(args[i].toLowerCase().equals("-args"))
                    {
                        if(jvmArgs)
                            printUsage("JVM args defined mutiply");
                        jvmArgs=true;
                        i++;
                        jvmArgsP = args[i];
                    }
                    else if(args[i].toLowerCase().equals("-main"))
                    {
                        if(main)
                            printUsage("Main class identity defined multiply");
                        main=true;
                        i++;
                        mainP = args[i];
                    }
                    else
                    {
                        break;
                    }
                }
            }catch(java.lang.ArrayIndexOutOfBoundsException e){printUsage("Expected parameter missing");};
            if(!jars)
            {//User hasnt specified input jars
                printUsage("-jars input parameter must be used");
            }
            else
            {
                cleanse(jarsP, jarTypes);
                if(jarsP.length==0)
                {
                    printUsage("You must provide atleast 1 .jar after the -jars parameter.");
                }
            }
            if(!main)
            {//User hasnt specified input jars
                printUsage("-main input parameter must be used");
            }
            if(natives)
            {
                cleanse(nativesP, nativeTypes);
            }
            if(fat)
            {
                Splicer splicer = new Splicer();
                try
                {
                    splicer.createFatJar(jarsP, nativesP, fatP, mainP, jvmArgsP);
                    System.out.println("Fat .jar saved as: "+fatP);
                }
                catch (Exception e)
                {
                    System.err.println("Creation of fat .jar failed:");
                    System.err.println(e.toString());
                }
            }
            if(linux)
            {
                ShellScriptSplicer splicer = new ShellScriptSplicer();
                try
                {
                    splicer.createFatJar(jarsP, nativesP, linuxP, mainP, jvmArgsP);
                    System.out.println(".sh saved as: "+linuxP);
                }
                catch (Exception e)
                {
                    System.err.println("Creation of .sh failed:");
                    System.err.println(e.toString());
                }
            }
            if(mac)
            {
                MacAppSplicer splicer = new MacAppSplicer();
                try
                {
                    splicer.createAppBundle(jarsP, nativesP, macP, mainP, jvmArgsP, macnameP, macicon?maciconP:"");
                    System.out.println("APP bundle saved as: "+macP);
                }
                catch (Exception e)
                {
                    System.err.println("Creation of App bundle failed:");
                    System.err.println(e.toString());
                }
            }
            if(windows)
            {
                WinExeSplicer splicer = new WinExeSplicer();
                try
                {
                    splicer.createFatJar(jarsP, nativesP, windowsP, mainP, jvmArgsP);
                    System.out.println("Windows .exe saved as: "+fatP);
                }
                catch (Exception e)
                {
                    System.err.println("Creation of windows .exe failed:");
                    System.err.println(e.toString());
                }
            }
        }
    }
    public static void printUsage(String output)
    {
        System.out.println(output);
        System.out.println("Usage: java -jar jarsplice-0.40.jar <outputs> <inputs>");
        System.out.println("Where possible output parameters include: (Must specify atleast 1 output type)");
        System.out.println(" -jar <path>                 Specify where to place fat .jar if desired.");
        System.out.println(" -sh <path>                  Specify where to place .sh if desired.");
        System.out.println(" -exe <path>                 Specify where to place .exe if desired.");
        System.out.println(" -app <path> <name> [<icon>] Specify where to place .app if desired.");
        System.out.println("");
        System.out.println("Where the possible icon parameter is:");
        System.out.println(" -icon <path> \t      Specify where to find the .icns icon if desired.");
        System.out.println("");
        System.out.println("Where required input parameters include:");
        System.out.println(" -jars <path1>[;<path2>;<path3>..] Specify where to find the input .jar's.");
        System.out.println(" -nats <path1>[;<path2>;<path3>..] Specify where to find the input natives if desired.");
        System.out.println(" -main <class>                Specify the class containing the main method.");
        System.out.println();
        System.exit(1);
    }
    public static void cleanse(String[] input, String[] filter)
    {
        ArrayList<String> output = new ArrayList<String>();
        File temp;
        int counter;
        for(String i: input)
        {//For every input
            temp = new File(i);
            if(temp.exists())
            {//which exists
                counter = 0;
                for(String j: filter)
                {
                    if(i.toLowerCase().endsWith(j))
                    {//If it matches a filter, add it to output and stop checking it
                        output.add(i);
                        break;
                    }
                    else
                    {//Otherwise count upwards so we can see if it has been added
                        counter++;
                    }
                }
                if(counter==filter.length)
                    System.out.println("The file "+i+" is not of a valid filetype for its field, ignoring.");
            }
            else
            {
                System.out.println("The file "+i+" was not found, ignoring.");
            }
        }
        //Pass output back byref
        input = new String[output.size()];
        output.toArray(input);
    }
}