Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AwtScreenshot: Do not add "bin" to classpath #1486 #2433

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,60 +74,63 @@ public void run() {
}
}

public static void dumpAwtScreenshot(String screenshotFile) {
try {
URL location = AwtScreenshot.class.getProtectionDomain().getCodeSource().getLocation();
String cp = location.toURI().getPath();
if (new File(cp).isDirectory() && !cp.endsWith(File.separatorChar + "bin" + File.separatorChar)) {
cp += "bin" + File.separatorChar;
}
String javaHome = System.getProperty("java.home");
String javaExe = javaHome + File.separatorChar + "bin" + File.separatorChar + "java";
if (File.separatorChar == '\\') {
javaExe += ".exe"; // assume it's Windows
}
String[] args = new String[] { javaExe, "-cp", cp, AwtScreenshot.class.getName(), screenshotFile };
// System.out.println("Start process: " + Arrays.asList(args));
ProcessBuilder processBuilder = new ProcessBuilder(args);
if ("Mac OS X".equals(System.getProperty("os.name"))) {
processBuilder.environment().put("AWT_TOOLKIT", "CToolkit");
}
Process process = processBuilder.start();

@SuppressWarnings("resource") // never close process streams
InputStream errorStream = process.getErrorStream();

@SuppressWarnings("resource") // never close process streams
InputStream inputStream = process.getInputStream();

new StreamForwarder(errorStream, System.out).start();
new StreamForwarder(inputStream, System.out).start();
long end = System.currentTimeMillis() + TIMEOUT_SECONDS * 1000;
boolean done = false;
do {
try {
process.exitValue();
done = true;
} catch (IllegalThreadStateException e) {
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
// continue
}
}
} while (!done && System.currentTimeMillis() < end);

if (done) {
int exitCode = process.exitValue();
if (exitCode != 0) {
System.out.println("AwtScreenshot VM finished with exit code " + exitCode + ".");
}
} else {
process.destroy();
System.out.println("Killed AwtScreenshot VM after " + TIMEOUT_SECONDS + " seconds.");
}
} catch (URISyntaxException | IOException e) {
e.printStackTrace();
}
}
public static void dumpAwtScreenshot(String screenshotFile) {
try {
URL location = AwtScreenshot.class.getProtectionDomain().getCodeSource().getLocation();
String cp = location.toURI().getPath();
if (new File(cp).isDirectory() && !cp.endsWith(File.separatorChar + "bin" + File.separatorChar)
&& new File(cp + "bin" + File.separatorChar).isDirectory()) {
cp += "bin" + File.separatorChar;
}
String javaHome = System.getProperty("java.home");
String javaExe = javaHome + File.separatorChar + "bin" + File.separatorChar + "java";
if (File.separatorChar == '\\') {
javaExe += ".exe"; // assume it's Windows
}
String[] args = new String[] { javaExe, "-cp", cp, AwtScreenshot.class.getName(), screenshotFile };
// System.out.println("Start process: " + Arrays.asList(args));
ProcessBuilder processBuilder = new ProcessBuilder(args);
if ("Mac OS X".equals(System.getProperty("os.name"))) {
processBuilder.environment().put("AWT_TOOLKIT", "CToolkit");
}
Process process = processBuilder.start();

@SuppressWarnings("resource") // never close process streams
InputStream errorStream = process.getErrorStream();

@SuppressWarnings("resource") // never close process streams
InputStream inputStream = process.getInputStream();

new StreamForwarder(errorStream, System.out).start();
new StreamForwarder(inputStream, System.out).start();
long end = System.currentTimeMillis() + TIMEOUT_SECONDS * 1000;
boolean done = false;
do {
try {
process.exitValue();
done = true;
} catch (IllegalThreadStateException e) {
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
// continue
}
}
} while (!done && System.currentTimeMillis() < end);

if (done) {
int exitCode = process.exitValue();
if (exitCode != 0) {
new RuntimeException("AwtScreenshot VM finished with exit code " + exitCode + ".")
.printStackTrace();
}
} else {
process.destroy();
new RuntimeException("Killed AwtScreenshot VM after " + TIMEOUT_SECONDS + " seconds.")
.printStackTrace();
}
} catch (URISyntaxException | IOException e) {
e.printStackTrace();
}
}
}
Loading