Get executable file name in Java?

Is it possible to get the name of the file that is running. Like, for example, If I run a Jar file, and inside I want to add code functionality that can determine the file name, so that if this jar is renamed, the code should be able to detect it.

Is it possible to do this without scanning files in the current directory?

+6
source share
2 answers

Try the following:

String path = Test.class.getProtectionDomain().getCodeSource().getLocation().getPath(); String decodedPath = URLDecoder.decode(path, "UTF-8"); 

http://www.rgagnon.com/javadetails/java-0300.html

http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getProtectionDomain ()

http://docs.oracle.com/javase/7/docs/api/java/security/ProtectionDomain.html

+4
source

you can check if System.getEnv () has any value indicating which file was called

-2
source

Source: https://habr.com/ru/post/905802/


All Articles