Java-Programme neustarten

By |

Im Internet taucht immer wieder die Frage auf, wie man Java-Programme im .jar-Format neustarten kann. Lösungen sind allerdings rar. Also habe ich mich umgeschaut und mit den vorhandenen Informationen eine lauffähige Version geschrieben.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
String javaBin = System.getProperty("java.home") + "/bin/java";
File jarFile;
try{
    jarFile = new File
    (getClass().getProtectionDomain()
    .getCodeSource().getLocation().toURI());
    } catch(Exception e) {
    return;
}
if ( !jarFile.getName().endsWith(".jar") )
return;
 
String toExec[] = new String[] { javaBin, "-jar", jarFile.getPath() };
try{
    @SuppressWarnings("unused")
    Process p = Runtime.getRuntime().exec( toExec );
    } catch(Exception e) {
    e.printStackTrace();
    return;
}
 
System.exit(0);