|
1 | 1 | /*******************************************************************************
|
2 |
| - * Copyright (c) 2015, 2017 IBM Corporation and others. |
| 2 | + * Copyright (c) 2015, 2016 IBM Corporation and others. |
3 | 3 | *
|
4 | 4 | * This program and the accompanying materials
|
5 | 5 | * are made available under the terms of the Eclipse Public License 2.0
|
|
11 | 11 | * Contributors:
|
12 | 12 | * IBM Corporation - initial API and implementation
|
13 | 13 | *******************************************************************************/
|
| 14 | + |
14 | 15 | package org.eclipse.test;
|
15 | 16 |
|
16 | 17 | import java.awt.AWTException;
|
|
19 | 20 | import java.awt.Robot;
|
20 | 21 | import java.awt.Toolkit;
|
21 | 22 | import java.awt.image.BufferedImage;
|
| 23 | +import java.io.BufferedReader; |
22 | 24 | import java.io.File;
|
23 | 25 | import java.io.IOException;
|
| 26 | +import java.io.InputStream; |
| 27 | +import java.io.InputStreamReader; |
| 28 | +import java.io.PrintStream; |
| 29 | +import java.net.URISyntaxException; |
| 30 | +import java.net.URL; |
24 | 31 |
|
25 | 32 | import javax.imageio.ImageIO;
|
26 | 33 |
|
27 | 34 | public class AwtScreenshot {
|
28 | 35 |
|
29 |
| - public static void main(String[] args) { |
30 |
| - try { |
31 |
| - System.setProperty("java.awt.headless", "false"); |
32 |
| - Robot robot = new Robot(); |
33 |
| - Rectangle rect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); |
34 |
| - BufferedImage image = robot.createScreenCapture(rect); |
35 |
| - File file = new File(args[0]); |
36 |
| - ImageIO.write(image, "png", file); |
37 |
| - |
38 |
| - System.out.println("AWT screenshot saved to: " + file.getAbsolutePath()); |
39 |
| - } catch (HeadlessException | AWTException | IOException e) { |
40 |
| - e.printStackTrace(); |
41 |
| - } |
42 |
| - } |
| 36 | + private static final int TIMEOUT_SECONDS = 15; |
| 37 | + |
| 38 | + public static void main(String[] args) { |
| 39 | + try { |
| 40 | + System.setProperty("java.awt.headless", "false"); |
| 41 | + Robot robot = new Robot(); |
| 42 | + Rectangle rect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); |
| 43 | + BufferedImage image = robot.createScreenCapture(rect); |
| 44 | + File file = new File(args[0]); |
| 45 | + ImageIO.write(image, "png", file); |
| 46 | + |
| 47 | + System.out.println("AWT screenshot saved to: " + file.getAbsolutePath()); |
| 48 | + } catch (HeadlessException | AWTException | IOException e) { |
| 49 | + e.printStackTrace(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + static class StreamForwarder extends Thread { |
| 54 | + |
| 55 | + private InputStream fProcessOutput; |
| 56 | + |
| 57 | + private PrintStream fStream; |
| 58 | + |
| 59 | + public StreamForwarder(InputStream processOutput, PrintStream stream) { |
| 60 | + fProcessOutput = processOutput; |
| 61 | + fStream = stream; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void run() { |
| 66 | + try (BufferedReader reader = new BufferedReader(new InputStreamReader(fProcessOutput))) { |
| 67 | + String line = null; |
| 68 | + while ((line = reader.readLine()) != null) { |
| 69 | + fStream.println(line); |
| 70 | + } |
| 71 | + } catch (IOException e) { |
| 72 | + e.printStackTrace(); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public static void dumpAwtScreenshot(String screenshotFile) { |
| 78 | + try { |
| 79 | + URL location = AwtScreenshot.class.getProtectionDomain().getCodeSource().getLocation(); |
| 80 | + String cp = location.toURI().getPath(); |
| 81 | + if (new File(cp).isDirectory() && !cp.endsWith(File.separatorChar + "bin" + File.separatorChar)) { |
| 82 | + cp += "bin" + File.separatorChar; |
| 83 | + } |
| 84 | + String javaHome = System.getProperty("java.home"); |
| 85 | + String javaExe = javaHome + File.separatorChar + "bin" + File.separatorChar + "java"; |
| 86 | + if (File.separatorChar == '\\') { |
| 87 | + javaExe += ".exe"; // assume it's Windows |
| 88 | + } |
| 89 | + String[] args = new String[] { javaExe, "-cp", cp, AwtScreenshot.class.getName(), screenshotFile }; |
| 90 | + // System.out.println("Start process: " + Arrays.asList(args)); |
| 91 | + ProcessBuilder processBuilder = new ProcessBuilder(args); |
| 92 | + if ("Mac OS X".equals(System.getProperty("os.name"))) { |
| 93 | + processBuilder.environment().put("AWT_TOOLKIT", "CToolkit"); |
| 94 | + } |
| 95 | + Process process = processBuilder.start(); |
| 96 | + |
| 97 | + @SuppressWarnings("resource") // never close process streams |
| 98 | + InputStream errorStream = process.getErrorStream(); |
| 99 | + |
| 100 | + @SuppressWarnings("resource") // never close process streams |
| 101 | + InputStream inputStream = process.getInputStream(); |
| 102 | + |
| 103 | + new StreamForwarder(errorStream, System.out).start(); |
| 104 | + new StreamForwarder(inputStream, System.out).start(); |
| 105 | + long end = System.currentTimeMillis() + TIMEOUT_SECONDS * 1000; |
| 106 | + boolean done = false; |
| 107 | + do { |
| 108 | + try { |
| 109 | + process.exitValue(); |
| 110 | + done = true; |
| 111 | + } catch (IllegalThreadStateException e) { |
| 112 | + try { |
| 113 | + Thread.sleep(100); |
| 114 | + } catch (InterruptedException e1) { |
| 115 | + // continue |
| 116 | + } |
| 117 | + } |
| 118 | + } while (!done && System.currentTimeMillis() < end); |
43 | 119 |
|
| 120 | + if (done) { |
| 121 | + int exitCode = process.exitValue(); |
| 122 | + if (exitCode != 0) { |
| 123 | + System.out.println("AwtScreenshot VM finished with exit code " + exitCode + "."); |
| 124 | + } |
| 125 | + } else { |
| 126 | + process.destroy(); |
| 127 | + System.out.println("Killed AwtScreenshot VM after " + TIMEOUT_SECONDS + " seconds."); |
| 128 | + } |
| 129 | + } catch (URISyntaxException | IOException e) { |
| 130 | + e.printStackTrace(); |
| 131 | + } |
| 132 | + } |
44 | 133 | }
|
0 commit comments