Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get running file name in Java?

Is it possible to get the file name which is being executed. Like e.g. If i am running a Jar file, and inside i want to add code functionality that can detect the file name so that if this jar is renamed, code should be able to detect that.

Is there any possibility of doing this without scanning the files in current directory?

like image 481
Johnydep Avatar asked Jan 12 '12 09:01

Johnydep


1 Answers

Try this:

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

like image 150
Aleja_Vigo Avatar answered Oct 29 '22 21:10

Aleja_Vigo