11

Related to Find absolute java.exe path programatically from java code is there any way to know if the java process you're running was started as "java.exe" vs. "javaw.exe"?

Thanks

Community
  • 1
  • 1
rogerdpack
  • 62,887
  • 36
  • 269
  • 388

1 Answers1

13

If the VM has no Console available (say, because you started it with javaw.exe), then a call to System.console() will return null.

edit: i.e.,

final boolean amRunningJavaW = System.console() == null;
Ti Strga
  • 1,353
  • 18
  • 40
  • 1
    Strictly speaking, I should have named the variable `probablyRunningJavaW`, since this isn't actually testing the executable process name. :-) And there could be other reasons why a system Console isn't available, although I don't know of any offhand. – Ti Strga Feb 06 '13 at 17:53
  • 2
    With regards to reasons for `System.console()` returning `null`, if your application is run with its standard out stream piped to another program (e.g. more, less) or out to a file, then `System.console()` will return `null` – o.comp Apr 17 '17 at 12:31