简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM