Johnydep
Johnydep

Reputation: 6277

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?

Upvotes: 10

Views: 9307

Answers (2)

Aleja_Vigo
Aleja_Vigo

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

Hachi
Hachi

Reputation: 3289

you could check if System.getEnv() has some value indicating which file was invoked

Upvotes: -1

Related Questions