Bug fixes and startup checks
[debian/openrocket] / src / net / sf / openrocket / startup / Startup.java
1 package net.sf.openrocket.startup;
2
3 import java.awt.GraphicsEnvironment;
4 import java.lang.reflect.InvocationTargetException;
5 import java.lang.reflect.Method;
6
7 import javax.swing.JOptionPane;
8
9
10 /**
11  * A startup class that checks that a suitable JRE environment is being run.
12  * If the environment is too old the execution is canceled, and if OpenJDK is being
13  * used warns the user of problems and confirms whether to continue.
14  * <p>
15  * Note:  This class must be Java 1.4 compatible and calls the next class using
16  * only reflection.
17  * 
18  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
19  */
20 public class Startup {
21         
22         public static final String START_CLASS = "net.sf.openrocket.gui.main.BasicFrame";
23         
24         public static final int REQUIRED_MAJOR_VERSION = 1;
25         public static final int REQUIRED_MINOR_VERSION = 6;
26
27
28         public static void main(String[] args) {
29                 
30                 checkVersion();
31                 checkHead();
32                 checkOpenJDK();
33                 
34                 // Load and execute START_CLASS
35                 try {
36                         
37                         Class cls = Class.forName(START_CLASS);
38                         Method m = cls.getMethod("main", String[].class);
39                         m.invoke(null, new Object[] { args });
40                         
41                 } catch (ClassNotFoundException e) {
42                         e.printStackTrace();
43                         error("Error starting main class!", "Please report a bug.");
44                 } catch (NoSuchMethodException e) {
45                         e.printStackTrace();
46                         error("Error starting main class!", "Please report a bug.");
47                 } catch (InvocationTargetException e) {
48                         e.printStackTrace();
49                         error("Error starting main class!", "Please report a bug.");
50                 } catch (IllegalAccessException e) {
51                         e.printStackTrace();
52                         error("Error starting main class!", "Please report a bug.");
53                 }
54
55         }
56         
57         
58         /**
59          * Check that the JRE version is high enough.
60          */
61         private static void checkVersion() {
62                 String[] version = System.getProperty("java.specification.version", "").split("\\.");
63                 
64                 String jreName = System.getProperty("java.vm.name", "(unknown)");
65                 String jreVersion = System.getProperty("java.runtime.version", "(unknown)");
66                 String jreVendor = System.getProperty("java.vendor", "(unknown)");
67                 
68                 int major, minor;
69
70                 try {
71                         major = Integer.parseInt(version[0]);
72                         minor = Integer.parseInt(version[1]);
73                         
74                         if (major < REQUIRED_MAJOR_VERSION || 
75                                         (major == REQUIRED_MAJOR_VERSION && minor < REQUIRED_MINOR_VERSION)) {
76                                 error("Java SE version 6 is required to run OpenRocket.",
77                                                 "You are currently running " + jreName + " version " +
78                                                 jreVersion + " by " + jreVendor);
79                         }
80                         
81                 } catch (RuntimeException e) {
82                         
83                         confirm("The Java version in use could not be detected.",
84                                         "OpenRocket requires at least Java SE 6.",
85                                         "Continue anyway?");
86                         
87                 }
88                 
89         }
90
91         
92         /**
93          * Check that the JRE is not running headless.
94          */
95         private static void checkHead() {
96                 
97                 if (GraphicsEnvironment.isHeadless()) {
98                         System.err.println();
99                         System.err.println("OpenRocket cannot currently be run without the graphical " +
100                                         "user interface.");
101                         System.err.println();
102                         System.exit(1);
103                 }
104                 
105         }
106         
107         
108         /**
109          * Check whether OpenJDK is being used, and if it is warn the user about
110          * problems and confirm whether to continue.
111          */
112         private static void checkOpenJDK() {
113                 
114                 if (System.getProperty("java.runtime.name", "").toLowerCase().indexOf("icedtea")>=0 ||
115                                 System.getProperty("java.vm.name", "").toLowerCase().indexOf("openjdk")>=0) {
116
117                         String jreName = System.getProperty("java.vm.name", "(unknown)");
118                         String jreVersion = System.getProperty("java.runtime.version", "(unknown)");
119                         String jreVendor = System.getProperty("java.vendor", "(unknown)");
120
121                         confirm("OpenJDK is known to have problems running OpenRocket.",
122                                         " ",
123                                         "You are currently running " + jreName + " version " +
124                                         jreVersion + " by " + jreVendor,
125                                         "Do you want to continue?");
126                         
127                 }
128         }
129
130         
131         
132         
133         /**
134          * Presents an error message to the user and exits the application.
135          * 
136          * @param message       an array of messages to present.
137          */
138         private static void error(String ... message) {
139
140                 System.err.println();
141                 System.err.println("Error starting OpenRocket:");
142                 System.err.println();
143                 for (int i=0; i < message.length; i++) {
144                         System.err.println(message[i]);
145                 }
146                 System.err.println();
147
148                 
149                 if (!GraphicsEnvironment.isHeadless()) {
150
151                         JOptionPane.showMessageDialog(null, message, "Error starting OpenRocket",
152                                         JOptionPane.ERROR_MESSAGE);
153                         
154                 }
155                 
156                 System.exit(1);
157         }
158         
159         
160         /**
161          * Presents the user with a message dialog and asks whether to continue.
162          * If the user does not select "Yes" the the application exits.
163          * 
164          * @param message       the message Strings to show.
165          */
166         private static void confirm(String ... message) {
167
168                 if (!GraphicsEnvironment.isHeadless()) {
169                         
170                         if (JOptionPane.showConfirmDialog(null, message, "Error starting OpenRocket",
171                                         JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
172                                 System.exit(1);
173                         }
174                         
175                 }
176
177         }
178         
179         
180 }