001package apps;
002
003import jmri.util.swing.JmriJOptionPane;
004
005/**
006 * Check Java version during startup and complain if not current enough
007 *
008 * @author Bob Jacobsen Copyright 2021
009 */
010public class JavaVersionCheckWindow {
011
012    enum Compatibility {
013        SUPPORTED,
014        INCOMPATIBLE,
015        UNSUPPORTED; // Maybe yes, maybe no
016    }
017    /**
018     * @return true if OK
019     */
020    static Compatibility checkJavaVersion() {
021
022        String version = System.getProperty("java.version");
023        switch (version) {
024            case "11":
025            case "12":
026            case "13":
027                return Compatibility.SUPPORTED;
028            default:
029                if (version.startsWith("1.8")) {
030                    return Compatibility.INCOMPATIBLE;
031                } else {
032                    switch (version) {
033                        case "17":
034                            return Compatibility.UNSUPPORTED;
035                        default:
036                            return Compatibility.UNSUPPORTED;
037                    }
038                }
039        }
040    }
041
042    public static void main(String[] opts) {
043        Compatibility result = checkJavaVersion();
044        if (result == Compatibility.INCOMPATIBLE) {
045            System.err.println("JMRI cannot run on Java version "+System.getProperty("java.version"));
046            JmriJOptionPane.showMessageDialog(null, "JMRI cannot run on Java version "+System.getProperty("java.version"), "Alert", JmriJOptionPane.ERROR_MESSAGE);
047            System.exit(-1);
048        }
049    }
050}