How to check if the application is running on the command line?

I would like to write a Java application that opens a GUI if it starts from the GUI and parses the command line arguments if it is launched from the command line.

Is there any way to check if the application is running from the GUI?

+4
source share
1 answer

The console()class method Systemreturns an object Consoleif a console device is present, otherwise it returns null. The expression args.length > 0checks if argsall elements are in the String array .

 public static void main(String[] args) {
  Console console = System.console();
   if (console != null || args.length > 0) {
    // The app was started from a terminal
   }
 }
+7
source

All Articles