I would like to write a Java application that opens a GUI if started from a GUI and parses command line arguments if started from the command line.
Is there any way to check if the application was started from the GUI?
The console()
method of the System
class returns a Console
object if a console device is present, otherwise it returns null
. The expression args.length > 0
checks if the String array args
has any elements.
public static void main(String[] args) {
Console console = System.console();
if (console != null || args.length > 0) {
// The app was started from a terminal
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With