如何检查应用程序是否在命令行上运行?

Ger*_*ann 4 java console

我想编写一个Java应用程序,如果从GUI启动,则打开GUI,如果从命令行启动,则解析命令行参数.

有没有办法检查应用程序是否从GUI启动?

Ger*_*ann 7

如果存在控制台设备console(),则System该类的方法返回Console对象,否则返回null.表达式args.length > 0检查String数组args是否包含任何元素.

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