3
3
* SPDX-License-Identifier: LGPL-2.1-only
4
4
*/
5
5
package net .minecraftforge .installer ;
6
+ import java .awt .HeadlessException ;
6
7
import java .io .BufferedOutputStream ;
7
8
import java .io .File ;
8
9
import java .io .FileNotFoundException ;
@@ -122,8 +123,8 @@ public static void main(String[] args) throws IOException, URISyntaxException {
122
123
123
124
try {
124
125
if (!badCerts .isEmpty ()) {
125
- monitor .message ("Failed to validate certificates for " + badCerts + " this typically means you have an outdated java ." );
126
- monitor .message ("If instalation fails try updating your java !" );
126
+ monitor .message ("Failed to validate certificates for " + badCerts + " - this typically means you have an outdated Java ." );
127
+ monitor .message ("If installation fails, try updating your Java !" );
127
128
return ;
128
129
}
129
130
@@ -151,7 +152,7 @@ public static void main(String[] args) throws IOException, URISyntaxException {
151
152
}
152
153
153
154
if (!didWork )
154
- launchGui (monitor , installer , badCerts );
155
+ launchGui (monitor , installer , badCerts , parser );
155
156
}
156
157
157
158
private static OptionSpec <File > action (OptionParser parser , String arg , String desc ) {
@@ -169,10 +170,29 @@ else if (osType.contains("mac"))
169
170
return new File (userHomeDir , mcDir );
170
171
}
171
172
173
+ // Bouncer method, just in case
172
174
private static void launchGui (ProgressCallback monitor , File installer , String badCerts ) {
175
+ launchGui (monitor , installer , badCerts , null );
176
+ }
177
+
178
+ private static void launchGui (ProgressCallback monitor , File installer , String badCerts , OptionParser parser ) {
173
179
try {
174
180
UIManager .setLookAndFeel (UIManager .getSystemLookAndFeelClassName ());
175
- } catch (Exception e ) { }
181
+ } catch (HeadlessException headless ) {
182
+ // if ran in a headless CLI environment with no args, show some help text and exit gracefully
183
+ if (parser == null ) {
184
+ System .out .println ("No options found. Try running with --help for a list of options." );
185
+ System .exit (0 );
186
+ return ;
187
+ }
188
+ try {
189
+ parser .printHelpOn (System .out );
190
+ } catch (IOException ioException ) {
191
+ sneak (ioException );
192
+ }
193
+ System .exit (0 );
194
+ return ;
195
+ } catch (Exception ignored ) {}
176
196
177
197
try {
178
198
InstallV1 profile = Util .loadInstallProfile ();
@@ -227,4 +247,9 @@ public void write(int b) {
227
247
System .setOut (new PrintStream (monitorStream ));
228
248
System .setErr (new PrintStream (monitorStream ));
229
249
}
250
+
251
+ @ SuppressWarnings ("unchecked" )
252
+ private static <T extends Throwable > void sneak (Throwable t ) throws T {
253
+ throw (T ) t ;
254
+ }
230
255
}
0 commit comments