Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if application runs on the command line?

Tags:

java

console

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?

like image 589
Gerome Bochmann Avatar asked Mar 16 '23 20:03

Gerome Bochmann


1 Answers

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
   }
 }
like image 58
Gerome Bochmann Avatar answered Apr 27 '23 12:04

Gerome Bochmann