@@ -52,11 +52,29 @@ public static void main(String[] args) throws IOException, InterruptedException
5252 Files .copy (is , jarPath );
5353 System .out .println (colour ("Update to '" + url + "' successful! Re-starting the launcher..." ));
5454
55- new ProcessBuilder (resolveScript (relaunchCmd ))
56- .inheritIO ()
57- .start ();
58-
59- System .exit (0 );
55+ final var os = getOS ();
56+ final var scriptPath = resolveScriptPath (os );
57+ final var restartCmd = resolveScript (getOS (), relaunchCmd );
58+ if (!os .contains ("win" )) { // Not windows, so we need to make the file executable
59+ final var process = new ProcessBuilder ("chmod" , "+x" , scriptPath .toString ())
60+ .inheritIO ()
61+ .start ();
62+ process .onExit ().whenComplete (($ , $$ ) -> {
63+ try {
64+ new ProcessBuilder (restartCmd )
65+ .inheritIO ()
66+ .start ();
67+ } catch (IOException e ) {
68+ throw new RuntimeException (e );
69+ }
70+ System .exit (0 );
71+ });
72+ } else {
73+ new ProcessBuilder (restartCmd )
74+ .inheritIO ()
75+ .start ();
76+ System .exit (0 );
77+ }
6078 }
6179 }
6280
@@ -69,17 +87,19 @@ public static String readAllLines(InputStream is) throws IOException {
6987 return new String (is .readAllBytes ());
7088 }
7189
72- public static List < String > resolveScript (String script ) throws IOException {
90+ public static Path resolveScriptPath (String os ) {
7391 final var directory = Path .of (".relauncher" );
92+ return os .contains ("win" ) ? directory .resolve ("relaunch.bat" ).toAbsolutePath () : directory .resolve ("relaunch.sh" ).toAbsolutePath ();
93+ }
94+
95+ public static List <String > resolveScript (String os , String script ) throws IOException {
7496 var scriptFull = readAllLines (Objects .requireNonNull (SelfUpdate .class .getResourceAsStream ("/relauncher-restart" )));
75- final Path path ;
97+ final Path path = resolveScriptPath ( os ) ;
7698 final List <String > cmd ;
77- if (System .getProperty ("os.name" ).toLowerCase (Locale .ROOT ).contains ("win" )) {
78- path = directory .resolve ("relaunch.bat" ).toAbsolutePath ();
99+ if (os .contains ("win" )) {
79100 scriptFull = scriptFull .formatted ("cmd /c " + script );
80101 cmd = List .of (path .toString ());
81102 } else {
82- path = directory .resolve ("relaunch.sh" ).toAbsolutePath ();
83103 scriptFull = scriptFull .formatted (script );
84104 cmd = List .of ("sh" , path .toString ());
85105 }
@@ -89,4 +109,7 @@ public static List<String> resolveScript(String script) throws IOException {
89109 return cmd ;
90110 }
91111
112+ private static String getOS () {
113+ return System .getProperty ("os.name" ).toLowerCase (Locale .ROOT );
114+ }
92115}
0 commit comments