简体   繁体   English

引用执行jar的位置

[英]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. 我已经将其打包为eclipse的可运行jar文件,效果很好。 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. 问题是,将在存在该jar的目录中存在一个属性文件,而从我的java类之一中,我必须引用此属性文件。 The location of the jar file and property file can be dynamic but, they will be in same directory. jar文件和属性文件的位置可以是动态的,但是它们将位于同一目录中。 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. 该路径为您提供了Jar的目录或“路径部分”。 If you want the name of the Jar file you need getFile() as well. 如果需要Jar文件的名称,则还需要getFile()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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