AC_1985
AC_1985

Reputation: 193

Referring executing jar location

I have seen similar question in this forum already but none of the answers are working for me. Here is what I'm trying to get: I have an application with multiple classes. I have packaged that as a runnable jar file from eclipse which works fine. The problem is, there will be a properties file present in the same directory where this jar will be present and from one of my java class I have to refer to this property file. The location of the jar file and property file can be dynamic but, they will be in same directory. To achieve this I have tried the following:

MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath()    

and

MyClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()

But all of this returns only ./ for my case. Please help.

Upvotes: 0

Views: 60

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533442

The path gives you the directory or "path part" of the Jar. If you want the name of the Jar file you need getFile() as well.

Upvotes: 1

Related Questions