package de.tutorials.java;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class ExeFromJarExecutor {
public static void main(String[] args) {
new ExeFromJarExecutor().launch();
}
private void launch() {
InputStream is =
ExeFromJarExecutor.class.getResourceAsStream("calc.exe");
BufferedInputStream bis = new BufferedInputStream(is);
File userHome =
new File(System.getProperties().get("user.home").toString());
String appPath =
userHome.getAbsolutePath() + File.separatorChar + "calc.exe";
System.out.println(appPath);
File appFile = new File(appPath);
try {
BufferedOutputStream bos =
new BufferedOutputStream(new FileOutputStream(appFile));
byte[] buffer = new byte[8192];
int len = 0;
while ((len = bis.read(buffer)) > 0) {
bos.write(buffer, 0, len);
}
bos.flush();
bos.close();
bis.close();
Process p = Runtime.getRuntime().exec(appPath);
p.getErrorStream().close();
p.getOutputStream().close();
p.getInputStream().close();
int exitCode = p.waitFor();
appFile.deleteOnExit();
System.out.println(exitCode);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}