|
1 | 1 | package de.littlerolf.sav;
|
2 | 2 |
|
| 3 | +import java.io.BufferedReader; |
3 | 4 | import java.io.File;
|
4 | 5 | import java.io.FileOutputStream;
|
5 | 6 | import java.io.IOException;
|
6 | 7 | import java.io.InputStream;
|
7 |
| -import java.lang.ProcessBuilder.Redirect; |
| 8 | +import java.io.InputStreamReader; |
8 | 9 | import java.net.MalformedURLException;
|
9 | 10 | import java.net.URL;
|
10 | 11 | import java.net.URLClassLoader;
|
@@ -32,17 +33,21 @@ public static void main(String[] args) {
|
32 | 33 | int remoteVersion = getRemoteVersion();
|
33 | 34 | int localVersion = getLocalVersion();
|
34 | 35 | int downloadedVersion = getDownloadedJarVersion();
|
35 |
| - |
36 |
| - if (remoteVersion > downloadedVersion && remoteVersion > localVersion) { |
37 |
| - System.out.println("Starting remote version."); |
38 |
| - startJar(downloadRemoteJar().getAbsolutePath()); |
39 |
| - } else if (downloadedVersion > remoteVersion |
40 |
| - && downloadedVersion > localVersion) { |
41 |
| - System.out.println("Starting cached version."); |
42 |
| - startJar(DOWNLOADED_FILE.getAbsolutePath()); |
43 |
| - } else { |
44 |
| - System.out.println("Starting local version."); |
| 36 | + if (localVersion == -1) { |
45 | 37 | startLocal();
|
| 38 | + } else { |
| 39 | + if (remoteVersion > downloadedVersion |
| 40 | + && remoteVersion > localVersion) { |
| 41 | + System.out.println("Starting remote version."); |
| 42 | + startJar(downloadRemoteJar().getAbsolutePath()); |
| 43 | + } else if (downloadedVersion > remoteVersion |
| 44 | + && downloadedVersion > localVersion) { |
| 45 | + System.out.println("Starting cached version."); |
| 46 | + startJar(DOWNLOADED_FILE.getAbsolutePath()); |
| 47 | + } else { |
| 48 | + System.out.println("Starting local version."); |
| 49 | + startLocal(); |
| 50 | + } |
46 | 51 | }
|
47 | 52 | /*
|
48 | 53 | * if (getLocalVersion() > -1) { if (isRemoteNewer()) {
|
@@ -149,10 +154,11 @@ private static void startJar(String path) {
|
149 | 154 | System.out.println("Starting jar " + path + ".");
|
150 | 155 | ProcessBuilder pb = new ProcessBuilder("java", "-classpath", path,
|
151 | 156 | SortAlgorithmVisualizer.class.getName());
|
152 |
| - pb.redirectInput(Redirect.INHERIT).redirectOutput(Redirect.INHERIT) |
153 |
| - .redirectError(Redirect.INHERIT); |
154 | 157 | try {
|
155 | 158 | Process p = pb.start();
|
| 159 | + new StreamGobbler(p.getErrorStream()).start(); |
| 160 | + new StreamGobbler(p.getInputStream()).start(); |
| 161 | + |
156 | 162 | p.waitFor();
|
157 | 163 |
|
158 | 164 | } catch (IOException e) {
|
@@ -191,4 +197,25 @@ private static void startLocal() {
|
191 | 197 | JOptionPane.ERROR_MESSAGE);
|
192 | 198 | }
|
193 | 199 | }
|
| 200 | + |
| 201 | + private static class StreamGobbler extends Thread { |
| 202 | + InputStream is; |
| 203 | + |
| 204 | + // reads everything from is until empty. |
| 205 | + StreamGobbler(InputStream is) { |
| 206 | + this.is = is; |
| 207 | + } |
| 208 | + |
| 209 | + public void run() { |
| 210 | + try { |
| 211 | + InputStreamReader isr = new InputStreamReader(is); |
| 212 | + BufferedReader br = new BufferedReader(isr); |
| 213 | + String line = null; |
| 214 | + while ((line = br.readLine()) != null) |
| 215 | + System.out.println(line); |
| 216 | + } catch (IOException ioe) { |
| 217 | + ioe.printStackTrace(); |
| 218 | + } |
| 219 | + } |
| 220 | + } |
194 | 221 | }
|
0 commit comments