Name: JarBinder
Description:
This filebinder that can bind runnable .jar and .exe files to a jar file.
It has a GUI as well as a CLI. If you start the program without any arguments, the GUI is invoked.
CLI-Usage:
Quote:usage: java -jar jarbinder <runnable-file-1> <runnable-file-2> <destination-file>
Example: java -jar jarbinder app1.jar app2.jar dest.jar
Example output:
Quote:-*-*-*------------JarBinder V.0.1 by Deque------------*-*-*-
binding files app1.jar and app2.jar to dest.jar ...
done
Requirements
You need a Java7 JDK. The program will try to find it in the default location. If it can't find any, it will prompt you to give the location. It will save the location in a settings file, so you only have to do this once.
Supported OS
I tested the program for Windows and Linux.
Coded in: Java
Screenshot:
Source: http://www.file-upload.net/download-6726...e.zip.html
Program: http://www.file-upload.net/download-4819...r.zip.html
Edit: I forgot the source of the stub which is only included as .class file.
Stub source:
Code:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.security.ProtectionDomain;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class Application {
private static final String SEP = System.getProperty("file.separator");
private static final int ZIP_MAGIC_NUMBER = 0x504B0304;
public static void main(final String[] args) throws URISyntaxException,
IOException, InterruptedException {
URI uri = getJarLocation();
URI runnable1 = createTempFile(uri, "GUI.class");
Process process1 = runFile(runnable1);
URI runnable2 = createTempFile(uri, "Properties.class");
Process process2 = runFile(runnable2);
// wait so tempFiles can be deleted afterwards
process1.waitFor();
process2.waitFor();
}
private static boolean isZipFile(File file) {
try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {
int n = raf.readInt();
return n == ZIP_MAGIC_NUMBER ? true : false;
} catch (IOException e) {
return false;
}
}
private static boolean osIsWindows() {
return System.getProperty("os.name").toLowerCase().contains("windows");
}
private static Process runFile(URI runnable) throws IOException {
File file = new File(runnable);
ProcessBuilder pb = null;
if (isZipFile(file)) {
pb = runAsJar(file);
} else {
pb = runAsExe(file);
}
return pb.start();
}
private static ProcessBuilder runAsExe(File file) {
ProcessBuilder pb;
if (osIsWindows()) {
pb = new ProcessBuilder(file.toString());
} else {
if (file.toString().startsWith("/")
|| file.toString().startsWith("./")) {
pb = new ProcessBuilder(file.toString());
} else {
pb = new ProcessBuilder("./" + file.toString());
}
}
return pb;
}
private static ProcessBuilder runAsJar(File file) {
String javaRunnable = "java";
if (osIsWindows()) {
javaRunnable = "java.exe";
}
ProcessBuilder pb = new ProcessBuilder(System.getProperty("java.home")
+ SEP + "bin" + SEP + javaRunnable, "-jar", file.toString());
return pb;
}
private static URI getJarLocation() throws URISyntaxException {
ProtectionDomain domain = Application.class.getProtectionDomain();
URL url = domain.getCodeSource().getLocation();
return url.toURI();
}
private static URI createTempFile(URI jarLocation, String fileToExtract)
throws IOException {
try (ZipFile zipFile = new ZipFile(new File(jarLocation))) {
File tempFile = File.createTempFile(
fileToExtract + Long.toString(System.currentTimeMillis()),
".exe");
setFilePermissions(tempFile);
tempFile.deleteOnExit();
ZipEntry entry = zipFile.getEntry(fileToExtract);
if (entry != null) {
return extract(entry, tempFile, zipFile);
} else {
throw new FileNotFoundException("File " + fileToExtract
+ " not found in " + zipFile.getName());
}
}
}
private static void setFilePermissions(File tempFile) throws IOException {
if (!osIsWindows()) {
Set<PosixFilePermission> perms = PosixFilePermissions
.fromString("rwxrwxrwx");
Files.setPosixFilePermissions(tempFile.toPath(), perms);
}
}
private static URI extract(ZipEntry entry, File tempFile, ZipFile zipFile)
throws IOException {
try (InputStream in = zipFile.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(tempFile)) {
byte[] buf = new byte[512];
int i;
while ((i = in.read(buf)) != -1) {
fos.write(buf, 0, i);
}
}
return tempFile.toURI();
}
}