Is there a way to know if a Java program was started from the command line or from a jar file?

前端 未结 7 1990
伪装坚强ぢ
伪装坚强ぢ 2021-02-03 20:45

I want to either display a message in the console or a pop up, so in case a parameter is not specified, I want to know to which should I display

Something like:

7条回答
  •  时光说笑
    2021-02-03 21:11

    it's true that it is impossible to tell how the JVM was invoked. but... there's a way to side step this. you assumed that when the user double clicked on a JAR, then there's GUI running... ok. so let's extend this assumption. check.. from where the class was invoked, the directory. check that directory.. assuming it's a normal usage, when there's a *.jar file, then the user must've started the app from a jar.. but one flaw is that the user can also click on the main class file. hahahaha

提交回复
热议问题