Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 7 additions & 22 deletions src/org/ninjacave/jarsplice/JarSpliceLauncher.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
package org.ninjacave.jarsplice;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.jar.*;

public class JarSpliceLauncher
{
Expand Down Expand Up @@ -130,15 +115,15 @@ public String getNativeDirectory() {
if ((nativeDir == null) || (System.getProperty("os.name").startsWith("Win"))) {
nativeDir = System.getProperty("java.io.tmpdir");
}

nativeDir = nativeDir + File.separator + "natives" + new Random().nextInt();
if(!nativeDir.endsWith(File.separator))
nativeDir+=File.separator;
nativeDir += "natives" + new Random().nextInt();

File dir = new File(nativeDir);

if (!dir.exists()) {
dir.mkdirs();
}

return nativeDir;
}

Expand Down
235 changes: 144 additions & 91 deletions src/org/ninjacave/jarsplice/core/Splicer.java
Original file line number Diff line number Diff line change
@@ -1,121 +1,174 @@
package org.ninjacave.jarsplice.core;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.jar.Attributes;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.ninjacave.jarsplice.JarSplice;
import java.io.*;
import java.util.*;
import java.util.jar.*;
import java.util.zip.*;
import org.ninjacave.jarsplice.*;

public class Splicer
{
ArrayList<String> dirs = new ArrayList();
int bufferSize;
byte[] buffer = new byte[4096];

public void createFatJar(String[] jars, String[] natives, String output, String mainClass, String vmArgs) throws Exception {
this.dirs.clear();
ArrayList<String> dirs = new ArrayList();
int bufferSize;
byte[] buffer = new byte[4096];

Manifest manifest = getManifest(mainClass, vmArgs);

FileOutputStream fos = new FileOutputStream(output);
JarOutputStream jos = new JarOutputStream(fos, manifest);
try
public void createFatJar(String[] jars, String[] natives, String output, String mainClass, String vmArgs) throws Exception
{
addFilesFromJars(jars, jos);
addNativesToJar(natives, jos);
addJarSpliceLauncher(jos);
this.dirs.clear();
services.clear();

Manifest manifest = getManifest(mainClass, vmArgs);

FileOutputStream fos = new FileOutputStream(output);
JarOutputStream jos = new JarOutputStream(fos, manifest);
try
{
addFilesFromJars(jars, jos);
addNativesToJar(natives, jos);
addJarSpliceLauncher(jos);
processCachedServices(jos);
}
finally
{
jos.close();
fos.close();
}
}
finally {
jos.close();
fos.close();

protected Manifest getManifest(String mainClass, String vmArgs)
{
Manifest manifest = new Manifest();
Attributes attribute = manifest.getMainAttributes();
attribute.putValue("Manifest-Version", "1.0");
attribute.putValue("Main-Class", "org.ninjacave.jarsplice.JarSpliceLauncher");
attribute.putValue("Launcher-Main-Class", mainClass);
attribute.putValue("Launcher-VM-Args", vmArgs);

return manifest;
}
}

protected Manifest getManifest(String mainClass, String vmArgs) {
Manifest manifest = new Manifest();
Attributes attribute = manifest.getMainAttributes();
attribute.putValue("Manifest-Version", "1.0");
attribute.putValue("Main-Class", "org.ninjacave.jarsplice.JarSpliceLauncher");
attribute.putValue("Launcher-Main-Class", mainClass);
attribute.putValue("Launcher-VM-Args", vmArgs);

return manifest;
}

protected void addFilesFromJars(String[] jars, JarOutputStream out) throws Exception
{
for (int i = 0; i < jars.length; i++)

HashMap<String, String> services = new HashMap<>();

private void cacheService(String filename, InputStream is)
{
ZipFile jarFile = new ZipFile(jars[i]);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder out = new StringBuilder();
String line;
try
{
while ((line = reader.readLine()) != null)
{
out.append(line).append("\n");
}
}
catch (IOException ex)
{
}

Enumeration entities = jarFile.entries();
if (services.containsKey(filename))
{
out.append(services.get(filename));
services.replace(filename, out.toString());
}
else
{
services.put(filename, out.toString());
}
}

while (entities.hasMoreElements()) {
ZipEntry entry = (ZipEntry)entities.nextElement();
private void processCachedServices(JarOutputStream out) throws IOException
{
for (String key : services.keySet())
{
System.out.print(key);
out.putNextEntry(new ZipEntry(key));
out.write(services.get(key).getBytes());
out.closeEntry();
}
}

if (entry.isDirectory()) {
if (!this.dirs.contains(entry.getName()))
{
this.dirs.add(entry.getName());
}
protected void addFilesFromJars(String[] jars, JarOutputStream out) throws Exception
{
for (int i = 0; i < jars.length; i++)
{
ZipFile jarFile = new ZipFile(jars[i]);

Enumeration entities = jarFile.entries();

while (entities.hasMoreElements())
{
ZipEntry entry = (ZipEntry)entities.nextElement();
boolean isService = entry.getName().toLowerCase().contains("meta-inf/services/");

if (entry.isDirectory())
{
if (!this.dirs.contains(entry.getName()))
{
this.dirs.add(entry.getName());
}
}
else if ((!entry.getName().toLowerCase().startsWith("meta-inf") || isService))
{
if (isService)
{
InputStream in = jarFile.getInputStream(jarFile.getEntry(entry.getName()));
cacheService(entry.getName(), jarFile.getInputStream(jarFile.getEntry(entry.getName())));
in.close();
}
else if (!entry.getName().toLowerCase().contains("JarSpliceLauncher"))
{
InputStream in = jarFile.getInputStream(jarFile.getEntry(entry.getName()));

out.putNextEntry(new ZipEntry(entry.getName()));
while ((this.bufferSize = in.read(this.buffer, 0, this.buffer.length)) != -1)
{
out.write(this.buffer, 0, this.bufferSize);
}

in.close();
out.closeEntry();
}
}
}
jarFile.close();
}
else if (!entry.getName().toLowerCase().startsWith("meta-inf"))
}

protected void addNativesToJar(String[] natives, JarOutputStream out) throws Exception
{
for (int i = 0; i < natives.length; i++)
{
if (!entry.getName().toLowerCase().contains("JarSpliceLauncher"))
{
InputStream in = jarFile.getInputStream(jarFile.getEntry(entry.getName()));
InputStream in = new FileInputStream(natives[i]);

out.putNextEntry(new ZipEntry(entry.getName()));
out.putNextEntry(new ZipEntry(getFileName(natives[i])));

while ((this.bufferSize = in.read(this.buffer, 0, this.buffer.length)) != -1) {
out.write(this.buffer, 0, this.bufferSize);
while ((this.bufferSize = in.read(this.buffer, 0, this.buffer.length)) != -1)
{
out.write(this.buffer, 0, this.bufferSize);
}

in.close();
out.closeEntry();
}
}
}
jarFile.close();
}
}

protected void addNativesToJar(String[] natives, JarOutputStream out) throws Exception
{
for (int i = 0; i < natives.length; i++) {
InputStream in = new FileInputStream(natives[i]);

out.putNextEntry(new ZipEntry(getFileName(natives[i])));

while ((this.bufferSize = in.read(this.buffer, 0, this.buffer.length)) != -1) {
out.write(this.buffer, 0, this.bufferSize);
}
protected void addJarSpliceLauncher(JarOutputStream out) throws Exception
{
InputStream in = JarSplice.class.getResourceAsStream("JarSpliceLauncher.class");

in.close();
out.closeEntry();
out.putNextEntry(new ZipEntry("org/ninjacave/jarsplice/JarSpliceLauncher.class"));
while ((this.bufferSize = in.read(this.buffer, 0, this.buffer.length)) != -1)
{
out.write(this.buffer, 0, this.bufferSize);
}
in.close();
out.closeEntry();
}
}

protected void addJarSpliceLauncher(JarOutputStream out) throws Exception
{
InputStream in = JarSplice.class.getResourceAsStream("JarSpliceLauncher.class");

out.putNextEntry(new ZipEntry("org/ninjacave/jarsplice/JarSpliceLauncher.class"));
while ((this.bufferSize = in.read(this.buffer, 0, this.buffer.length)) != -1) {
out.write(this.buffer, 0, this.bufferSize);
protected String getFileName(String ref)
{
ref = ref.replace('\\', '/');
return ref.substring(ref.lastIndexOf('/') + 1);
}
in.close();
out.closeEntry();
}

protected String getFileName(String ref) {
ref = ref.replace('\\', '/');
return ref.substring(ref.lastIndexOf('/') + 1);
}
}
32 changes: 13 additions & 19 deletions src/org/ninjacave/jarsplice/gui/CreatePanel.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
package org.ninjacave.jarsplice.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.PrintStream;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.filechooser.FileFilter;
import org.ninjacave.jarsplice.core.Splicer;
import org.ninjacave.jarsplice.core.*;

public class CreatePanel extends JPanel
implements ActionListener
Expand All @@ -28,9 +22,9 @@ public CreatePanel(JarSpliceFrame jarSplice) {
public void approveSelection() {
File f = getSelectedFile();
if ((f.exists()) && (getDialogType() == 1)) {
int result =
int result =
JOptionPane.showConfirmDialog(
this, "The file already exists. Do you want to overwrite it?",
this, "The file already exists. Do you want to overwrite it?",
"Confirm Replace", 0);
switch (result) {
case 0:
Expand Down Expand Up @@ -85,9 +79,9 @@ public String getOutputFile(File file) {
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == this.createButton) {
this.fileChooser.setCurrentDirectory(this.jarSplice.lastDirectory);
this.fileChooser.setCurrentDirectory(this.jarSplice.lastExportDirectory);
int value = this.fileChooser.showSaveDialog(this);
this.jarSplice.lastDirectory = this.fileChooser.getCurrentDirectory();
this.jarSplice.lastExportDirectory = this.fileChooser.getCurrentDirectory();

if (value == 0)
{
Expand All @@ -100,14 +94,14 @@ public void actionPerformed(ActionEvent e)
{
this.splicer.createFatJar(jars, natives, output, mainClass, vmArgs);

JOptionPane.showMessageDialog(this,
"Fat Jar Successfully Created.",
JOptionPane.showMessageDialog(this,
"Fat Jar Successfully Created.",
"Success", -1);
}
catch (Exception ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this,
"Jar creation failed due to the following exception:\n" + ex.getMessage(),
JOptionPane.showMessageDialog(this,
"Jar creation failed due to the following exception:\n" + ex.getMessage(),
"Failed", 0);
}

Expand Down
Loading