Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #36084

Re: How do I get the name and path of the running jar file?

Path csiph.com!aioe.org!.POSTED!not-for-mail
From Arne Vajhøj <arne@vajhoej.dk>
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 <o2ku8a$1gv3$1@gioia.aioe.org> (permalink)
References <0564c643-106a-499a-833e-dffe34d1e915@googlegroups.com> <o27ri1$bbj$1@gioia.aioe.org> <eat50vFk6idU1@mid.individual.net>
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

Show key headers only | View raw


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



Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How do I get the name and path of the running jar file? triciac.1999.p@gmail.com - 2016-12-06 12:41 -0800
  Re: How do I get the name and path of the running jar file? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-12-06 16:07 -0500
    Re: How do I get the name and path of the running jar file? Tricia Cahill <triciac.1999.p@gmail.com> - 2016-12-06 14:22 -0800
  Re: How do I get the name and path of the running jar file? Arne Vajhøj <arne@vajhoej.dk> - 2016-12-06 21:18 -0500
    Re: How do I get the name and path of the running jar file? Nigel Wade <nmw@local.domain> - 2016-12-08 13:20 +0000
      Re: How do I get the name and path of the running jar file? Arne Vajhøj <arne@vajhoej.dk> - 2016-12-11 20:24 -0500
        Re: How do I get the name and path of the running jar file? Nigel Wade <nmw@local.domain> - 2016-12-12 09:51 +0000
          Re: How do I get the name and path of the running jar file? Arne Vajhøj <arne@vajhoej.dk> - 2016-12-12 20:25 -0500
    Re: How do I get the name and path of the running jar file? Wayne <wayne@nospam.invalid> - 2016-12-09 01:50 -0500
      Re: How do I get the name and path of the running jar file? Arne Vajhøj <arne@vajhoej.dk> - 2016-12-11 20:10 -0500
  Re: How do I get the name and path of the running jar file? Silvio <silvio@internet.com> - 2016-12-07 22:06 +0100
  Re: How do I get the name and path of the running jar file? lingmaaki@gmail.com - 2017-05-22 23:22 -0700

csiph-web