Hi!
I’m looking forward to build a simple Application Launcher that can be given any .jar to launch it with selected options. Games often need to have the heap memory initialized at a certain amount of RAM, as well as the permissions must be set up to the correct values.
Therefore I have an unique class that launch itself from the command line or the Operating system file browser. It is named AppLauncher with a common static main method that looks like the code follows:
/***/
public static void main(String[] args) {
String xms, xmx, codecsDisabled, openGL, ddscale, translaccel, ddforcevram, cwd;
System.out.println(System.getProperties());
xms = "64M";
xmx = "1024M";
codecsDisabled = "false";
openGL = "True";
translaccel = "true";
ddscale = "true";
ddforcevram = "true";
cwd = ".";
new UIMessage("AppLauncher will ask you to select the .jar to launch.", null);
FileChooserPane fcp = new FileChooserPane(null, ".");
HashSet ext = new HashSet();
ext.add("jar");
fcp.addFileFilter(ext);
fcp.setMultiSelectionEnabled(false);
fcp.setModeOpen(true);
File[] f = fcp.getFile();
new UIMessage("AppLauncher will ask you to select the policy file to use if available, or simply tap cancel.", null);
fcp.setMultiSelectionEnabled(false);
fcp.setModeOpen(true);
File[] policy = fcp.getFile();
if(f == null)
return;
else if(f[0] == null)
return;
JFrame frame = UIMessage.displayWaiting("Loading...", null);
Process p = null;
String command = "";
Runtime.getRuntime().traceInstructions(true);
try {if(System.getProperty("os.arch").equals("x86")) {
args = new String[]{
"-jar \"" + f[0].getCanonicalPath() + "\"",
(policy != null)?"-Djava.security.policy=\"" + policy[0].getCanonicalPath() + "\"":"",
"-Xms" + xms,
"-Xmx" + xmx,
"-Dcom.sun.media.imageio.disableCodecLib=" + codecsDisabled,
"-Dsun.java2d.opengl=" + openGL,
"-Dsun.java2d.ddscale=" + ddscale,
"-Dsun.java2d.translaccel=" + translaccel,
"-Dsun.java2d.ddforcevram=" + ddforcevram
};
command = "java";
} else if(System.getProperty("os.arch").equals("ppc")) {
args = new String[]{
"-jar \"" + f[0].getCanonicalPath() + "\"",
(policy != null)?"-Djava.security.policy=\"" + policy[0].getCanonicalPath() + "\"":"",
"-Xms" + xms,
"-Xmx" + xmx,
"-Dcom.sun.media.imageio.disableCodecLib=" + codecsDisabled,
"-Dsun.java2d.opengl=" + openGL,
"-Dsun.java2d.translaccel=" + translaccel
};
command = "java";
} else
new UIMessage(System.getProperty("os.arch") + " architectures are currently not supported. Program is going to exit...", null);
String argss = "";
for(String arg : args)
argss += arg + " ";
new UIMessage("LAUNCH : " + command + " " + argss, null);
p = Runtime.getRuntime().exec(command, args,
new File(cwd)
);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if(p instanceof Process) {
final Console console = new Console();
final BufferedInputStream err = new BufferedInputStream(p.getErrorStream());
Thread t = new Thread(new Runnable() { public void run(){
try{
Map<String, Object> map = console.newPrintStream(new ErrorListener() {
public void printStreamError(IOException e) {
e.printStackTrace();
}
});
PrintStream ps = ((PrintStream)map.get("ps"));
byte[] s = new byte[128];
while(err.read(s) != -1)
ps.append(new String(s));
} catch(IOException e) {
e.printStackTrace();
}
}}, "T-App Launcher ErrorLog");
t.setPriority(Thread.MAX_PRIORITY);
t.start();
console.sendConsoleEvent(Console.PRINT_RESUME);
frame.dispose();
try {
p.waitFor();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
System.exit(0);
}
But this doesn’t fill its purpose at all. it terminates with System.exit(0) and the java command is left uncompleted. Plus I can’t find out the possible exception thrown, where is the problem? I can agree that the command Runtime.getRuntime().exec() seems terribly tiny but I cannot find a different manner, can I? ??? ??? ??? ??? ???