21
21
import java .awt .Toolkit ;
22
22
import java .awt .image .BufferedImage ;
23
23
import java .io .BufferedReader ;
24
+ import java .io .ByteArrayOutputStream ;
24
25
import java .io .File ;
25
26
import java .io .IOException ;
26
27
import java .io .InputStream ;
27
28
import java .io .InputStreamReader ;
28
29
import java .io .PrintStream ;
29
30
import java .net .URISyntaxException ;
30
31
import java .net .URL ;
32
+ import java .util .concurrent .TimeUnit ;
31
33
32
34
import javax .imageio .ImageIO ;
33
35
@@ -47,6 +49,7 @@ public static void main(String[] args) {
47
49
System .out .println ("AWT screenshot saved to: " + file .getAbsolutePath ());
48
50
} catch (HeadlessException | AWTException | IOException e ) {
49
51
e .printStackTrace ();
52
+ System .exit (3 );
50
53
}
51
54
}
52
55
@@ -88,49 +91,39 @@ && new File(cp + "bin" + File.separatorChar).isDirectory()) {
88
91
javaExe += ".exe" ; // assume it's Windows
89
92
}
90
93
String [] args = new String [] { javaExe , "-cp" , cp , AwtScreenshot .class .getName (), screenshotFile };
91
- // System.out.println("Start process: " + Arrays.asList(args));
92
94
ProcessBuilder processBuilder = new ProcessBuilder (args );
93
95
if ("Mac OS X" .equals (System .getProperty ("os.name" ))) {
94
96
processBuilder .environment ().put ("AWT_TOOLKIT" , "CToolkit" );
95
97
}
96
98
Process process = processBuilder .start ();
97
-
98
- @ SuppressWarnings ("resource" ) // never close process streams
99
- InputStream errorStream = process .getErrorStream ();
100
-
101
- @ SuppressWarnings ("resource" ) // never close process streams
102
- InputStream inputStream = process .getInputStream ();
103
-
104
- new StreamForwarder (errorStream , System .out ).start ();
105
- new StreamForwarder (inputStream , System .out ).start ();
106
- long end = System .currentTimeMillis () + TIMEOUT_SECONDS * 1000 ;
107
- boolean done = false ;
108
- do {
99
+ try (InputStream errorStream = process .getErrorStream ();
100
+ InputStream inputStream = process .getInputStream ()) {
101
+ ByteArrayOutputStream errorOut = new ByteArrayOutputStream ();
102
+ new StreamForwarder (errorStream , new PrintStream (errorOut )).start ();
103
+ new StreamForwarder (inputStream , System .out ).start ();
109
104
try {
110
- process .exitValue ();
111
- done = true ;
112
- } catch (IllegalThreadStateException e ) {
113
- try {
114
- Thread .sleep (100 );
115
- } catch (InterruptedException e1 ) {
116
- // continue
117
- }
105
+ process .waitFor (TIMEOUT_SECONDS , TimeUnit .SECONDS );
106
+ } catch (InterruptedException ie ) {
107
+ ie .printStackTrace ();
118
108
}
119
- } while (!done && System .currentTimeMillis () < end );
120
109
121
- if (done ) {
122
- int exitCode = process .exitValue ();
123
- if (exitCode != 0 ) {
124
- new RuntimeException ("AwtScreenshot VM finished with exit code " + exitCode + "." )
125
- .printStackTrace ();
110
+ if (!process .isAlive ()) {
111
+ int exitCode = process .exitValue ();
112
+ if (exitCode != 0 ) {
113
+ throw new RuntimeException (
114
+ "AwtScreenshot VM finished with exit code " + exitCode + ":\n " + errorOut .toString ());
115
+ }
116
+ if (errorOut .size () > 0 ) {
117
+ System .out .println (errorOut .toString ());
118
+ }
119
+ } else {
120
+ process .destroy ();
121
+ throw new RuntimeException (
122
+ "Killed AwtScreenshot VM after " + TIMEOUT_SECONDS + " seconds:\n " + errorOut .toString ());
126
123
}
127
- } else {
128
- process .destroy ();
129
- new RuntimeException ("Killed AwtScreenshot VM after " + TIMEOUT_SECONDS + " seconds." )
130
- .printStackTrace ();
131
124
}
132
125
} catch (URISyntaxException | IOException e ) {
133
- e . printStackTrace ( );
126
+ throw new RuntimeException ( e );
134
127
}
135
128
}
136
129
}
0 commit comments