Merge pull request #1202 from SiboVG/issue-1176

[fixes #1176] Add JRE version checking at startup
This commit is contained in:
Joe Pfeiffer 2022-03-07 14:32:32 -07:00 committed by GitHub
commit c2a61c84db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 2 deletions

View File

@ -21,6 +21,9 @@ public final class Application {
private static Injector injector; private static Injector injector;
// Supported Java Runtime Environment versions in which OR is allowed to run (e.g. '11' for Java 11)
public static int[] SUPPORTED_JRE_VERSIONS = {11};
/** /**
* Return whether to use additional safety code checks. * Return whether to use additional safety code checks.
*/ */

View File

@ -5,7 +5,10 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.io.File; import java.io.File;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.Arrays;
import java.util.stream.IntStream;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import javax.swing.Timer; import javax.swing.Timer;
import javax.swing.ToolTipManager; import javax.swing.ToolTipManager;
@ -61,6 +64,11 @@ public class SwingStartup {
initializeLogging(); initializeLogging();
log.info("Starting up OpenRocket version {}", BuildProperties.getVersion()); log.info("Starting up OpenRocket version {}", BuildProperties.getVersion());
// Check JRE version
if (!checkJREVersion()) {
return;
}
// Check that we're not running headless // Check that we're not running headless
log.info("Checking for graphics head"); log.info("Checking for graphics head");
checkHead(); checkHead();
@ -85,6 +93,44 @@ public class SwingStartup {
} }
/**
* Checks whether the Java Runtime Engine version is supported.
*
* @return true if the JRE is supported, false if not
*/
private static boolean checkJREVersion() {
String JREVersion = System.getProperty("java.version");
if (JREVersion != null) {
try {
// We're only interested in the big decimal part of the JRE version
int version = Integer.parseInt(JREVersion.split("\\.")[0]);
if (IntStream.of(Application.SUPPORTED_JRE_VERSIONS).noneMatch(c -> c == version)) {
String title = "Unsupported Java version";
String message1 = "Unsupported Java version: %s";
String message2 = "Supported version(s): %s";
String message3 = "Please change the Java Runtime Environment version or install OpenRocket using a packaged installer.";
StringBuilder message = new StringBuilder();
message.append(String.format(message1, JREVersion));
message.append("\n");
String[] supported = Arrays.stream(Application.SUPPORTED_JRE_VERSIONS)
.mapToObj(String::valueOf)
.toArray(String[]::new);
message.append(String.format(message2, String.join(", ", supported)));
message.append("\n\n");
message.append(message3);
JOptionPane.showMessageDialog(null, message.toString(),
title, JOptionPane.ERROR_MESSAGE);
return false;
}
} catch (NumberFormatException e) {
log.warn("Malformed JRE version - " + JREVersion);
}
}
return true;
}
/** /**
* Set proper system properties if openrocket.debug is defined. * Set proper system properties if openrocket.debug is defined.
*/ */