Path: csiph.com!aioe.org!.POSTED!not-for-mail From: =?UTF-8?Q?Arne_Vajh=c3=b8j?= Newsgroups: comp.lang.java.programmer Subject: Re: How do I get the name and path of the running jar file? Date: Sun, 11 Dec 2016 20:24:29 -0500 Organization: Aioe.org NNTP Server Lines: 37 Message-ID: References: <0564c643-106a-499a-833e-dffe34d1e915@googlegroups.com> NNTP-Posting-Host: PBuS014lhzVyHjLbXmo/ZQ.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.5.1 X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.java.programmer:36084 On 12/8/2016 8:20 AM, Nigel Wade wrote: > On 07/12/16 02:18, Arne Vajhøj wrote: >> You will get the full name including path of the jar with one of the >> following: >> >> String path = >> MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath(); >> >> >> String path = >> MyClass.class.getClassLoader().getResource("mypackage/MyClass.class").getPath(); >> >> >> The format is sligthly different. The first is probably more convenient >> as you would not need to truncate at the '!'. > > Both methods CodeSource.getLocation() and ClassLoader.getResource() > return a URL, which will be URL encoded. So the path ought to be > URLdecode'd otherwise there may be problems with any characters in the > path which URL has encoded. > > Also, in the case of ClassLoader.getResource(), when executed from a jar > it will be of type "jar", and from class files it will be of type > "file". This will probably come into play when you run the code in an > IDE. NetBeans (I don't know about Eclipe) runs class files, not jars. > You will probably need to handle each case differently. Good points. But it should be possible to extract the information in the desired format. Arne