Reputation: 6277
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?
Upvotes: 10
Views: 9307
Reputation: 980
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
Upvotes: 5
Reputation: 3289
you could check if System.getEnv() has some value indicating which file was invoked
Upvotes: -1