2121import java .awt .Toolkit ;
2222import java .awt .image .BufferedImage ;
2323import java .io .BufferedReader ;
24+ import java .io .ByteArrayOutputStream ;
2425import java .io .File ;
2526import java .io .IOException ;
2627import java .io .InputStream ;
2728import java .io .InputStreamReader ;
2829import java .io .PrintStream ;
2930import java .net .URISyntaxException ;
3031import java .net .URL ;
32+ import java .util .concurrent .TimeUnit ;
3133
3234import javax .imageio .ImageIO ;
3335
@@ -47,6 +49,7 @@ public static void main(String[] args) {
4749 System .out .println ("AWT screenshot saved to: " + file .getAbsolutePath ());
4850 } catch (HeadlessException | AWTException | IOException e ) {
4951 e .printStackTrace ();
52+ System .exit (3 );
5053 }
5154 }
5255
@@ -88,49 +91,39 @@ && new File(cp + "bin" + File.separatorChar).isDirectory()) {
8891 javaExe += ".exe" ; // assume it's Windows
8992 }
9093 String [] args = new String [] { javaExe , "-cp" , cp , AwtScreenshot .class .getName (), screenshotFile };
91- // System.out.println("Start process: " + Arrays.asList(args));
9294 ProcessBuilder processBuilder = new ProcessBuilder (args );
9395 if ("Mac OS X" .equals (System .getProperty ("os.name" ))) {
9496 processBuilder .environment ().put ("AWT_TOOLKIT" , "CToolkit" );
9597 }
9698 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 ();
109104 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 ();
118108 }
119- } while (!done && System .currentTimeMillis () < end );
120109
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 ());
126123 }
127- } else {
128- process .destroy ();
129- new RuntimeException ("Killed AwtScreenshot VM after " + TIMEOUT_SECONDS + " seconds." )
130- .printStackTrace ();
131124 }
132125 } catch (URISyntaxException | IOException e ) {
133- e . printStackTrace ( );
126+ throw new RuntimeException ( e );
134127 }
135128 }
136129}
0 commit comments