Gerome Bochmann
Gerome Bochmann

Reputation: 494

How to check if application runs on the command line?

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?

Upvotes: 4

Views: 730

Answers (1)

Gerome Bochmann
Gerome Bochmann

Reputation: 494

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
   }
 }

Upvotes: 7

Related Questions