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


Groups > comp.lang.java.programmer > #36056 > unrolled thread

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

Started bytriciac.1999.p@gmail.com
First post2016-12-06 12:41 -0800
Last post2017-05-22 23:22 -0700
Articles 12 — 8 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  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

#36056 — How do I get the name and path of the running jar file?

Fromtriciac.1999.p@gmail.com
Date2016-12-06 12:41 -0800
SubjectHow do I get the name and path of the running jar file?
Message-ID<0564c643-106a-499a-833e-dffe34d1e915@googlegroups.com>
I am writing a program that has sound files with type names and numbers like, "win1.wav" and "lose3.wav". I want the program to get the name and path of the running jar file so I can programmatically choose a random sound file of a specified type that is packaged inside the jar file without having to hardcode it myself. Everything I've tried works in Eclipse, but not after it is exported because it doesn't find the name of the jar file, only the path. Is there a way I can get the name AND path of the jar file? Could I search through the jar file packages without using the name and path?

[toc] | [next] | [standalone]


#36057

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2016-12-06 16:07 -0500
Message-ID<o27987$d31$1@dont-email.me>
In reply to#36056
On 12/6/2016 3:41 PM, triciac.1999.p@gmail.com wrote:
> I am writing a program that has sound files with type names and numbers like, "win1.wav" and "lose3.wav". I want the program to get the name and path of the running jar file so I can programmatically choose a random sound file of a specified type that is packaged inside the jar file without having to hardcode it myself. Everything I've tried works in Eclipse, but not after it is exported because it doesn't find the name of the jar file, only the path. Is there a way I can get the name AND path of the jar file? Could I search through the jar file packages without using the name and path?

     Have you tried

	String classPath = System.getProperty("java.class.path");

?  For me, the class path *is* the path and name of the jar -- I guess
it might be more complicated if there are several jars involved, but
even then you could split the path on

	System.getProperty("path.separator")

... and look through the jars one by one.

-- 
esosman@comcast-dot-net.invalid
"Nobody ever went broke underestimating the intelligence of the
American public." -- HLM (paraphrased)

[toc] | [prev] | [next] | [standalone]


#36059

FromTricia Cahill <triciac.1999.p@gmail.com>
Date2016-12-06 14:22 -0800
Message-ID<e22da60d-7321-4fb3-8ee9-a4c3643a3d89@googlegroups.com>
In reply to#36057
Thank you. Thank you so much.

[toc] | [prev] | [next] | [standalone]


#36062

FromArne Vajhøj <arne@vajhoej.dk>
Date2016-12-06 21:18 -0500
Message-ID<o27ri1$bbj$1@gioia.aioe.org>
In reply to#36056
On 12/6/2016 3:41 PM, triciac.1999.p@gmail.com wrote:
> I am writing a program that has sound files with type names and
> numbers like, "win1.wav" and "lose3.wav". I want the program to get
> the name and path of the running jar file so I can programmatically
> choose a random sound file of a specified type that is packaged
> inside the jar file without having to hardcode it myself. Everything
> I've tried works in Eclipse, but not after it is exported because it
> doesn't find the name of the jar file, only the path. Is there a way
> I can get the name AND path of the jar file? Could I search through
> the jar file packages without using the name and path?

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 '!'.

But I doubt that you need it.

InputStream is = 
MyClass.class.getClassLoader().getResource("mypackage/foobar.wav");

should give you a stream to the file. Both when everything is
in the file system and when everything is in the jar file.

Arne

[toc] | [prev] | [next] | [standalone]


#36068

FromNigel Wade <nmw@local.domain>
Date2016-12-08 13:20 +0000
Message-ID<eat50vFk6idU1@mid.individual.net>
In reply to#36062
On 07/12/16 02:18, Arne Vajhøj wrote:
> On 12/6/2016 3:41 PM, triciac.1999.p@gmail.com wrote:
>> I am writing a program that has sound files with type names and
>> numbers like, "win1.wav" and "lose3.wav". I want the program to get
>> the name and path of the running jar file so I can programmatically
>> choose a random sound file of a specified type that is packaged
>> inside the jar file without having to hardcode it myself. Everything
>> I've tried works in Eclipse, but not after it is exported because it
>> doesn't find the name of the jar file, only the path. Is there a way
>> I can get the name AND path of the jar file? Could I search through
>> the jar file packages without using the name and path?
>
> 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.

> But I doubt that you need it.
>
> InputStream is = MyClass.class.getClassLoader().getResource("mypackage/foobar.wav");
>
> should give you a stream to the file. Both when everything is
> in the file system and when everything is in the jar file.
>

This is definitely the preferred method if you know which file you want to open.

[toc] | [prev] | [next] | [standalone]


#36084

FromArne Vajhøj <arne@vajhoej.dk>
Date2016-12-11 20:24 -0500
Message-ID<o2ku8a$1gv3$1@gioia.aioe.org>
In reply to#36068
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



[toc] | [prev] | [next] | [standalone]


#36086

FromNigel Wade <nmw@local.domain>
Date2016-12-12 09:51 +0000
Message-ID<eb7a97F2n09U1@mid.individual.net>
In reply to#36084
On 12/12/16 01:24, Arne Vajhøj wrote:
> 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.
>

It is definitely possible, but it's still good to be aware of the caveats before they cause you problems.

[toc] | [prev] | [next] | [standalone]


#36107

FromArne Vajhøj <arne@vajhoej.dk>
Date2016-12-12 20:25 -0500
Message-ID<o2nilo$1s70$1@gioia.aioe.org>
In reply to#36086
On 12/12/2016 4:51 AM, Nigel Wade wrote:
> On 12/12/16 01:24, Arne Vajhøj wrote:
>> On 12/8/2016 8:20 AM, Nigel Wade wrote:
>>> On 07/12/16 02:18, Arne Vajhøj wrote:
>>>> 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.
>
> It is definitely possible, but it's still good to be aware of the
> caveats before they cause you problems.

Absolutely.

Arne

[toc] | [prev] | [next] | [standalone]


#36073

FromWayne <wayne@nospam.invalid>
Date2016-12-09 01:50 -0500
Message-ID<o2dk4f$ibo$1@dont-email.me>
In reply to#36062
On 12/6/2016 9:18 PM, Arne Vajhøj wrote:
> ...
> 
> InputStream is = MyClass.class.getClassLoader().getResource("mypackage/foobar.wav");
> 
> should give you a stream to the file. Both when everything is
> in the file system and when everything is in the jar file.
> 
> Arne

I think you meant getResourceAsStream(); getResource returns a URL,
not an InputStream.

-- 
Wayne

[toc] | [prev] | [next] | [standalone]


#36083

FromArne Vajhøj <arne@vajhoej.dk>
Date2016-12-11 20:10 -0500
Message-ID<o2ktd6$1fui$1@gioia.aioe.org>
In reply to#36073
On 12/9/2016 1:50 AM, Wayne wrote:
> On 12/6/2016 9:18 PM, Arne Vajhøj wrote:
>> ...
>>
>> InputStream is = MyClass.class.getClassLoader().getResource("mypackage/foobar.wav");
>>
>> should give you a stream to the file. Both when everything is
>> in the file system and when everything is in the jar file.
>
> I think you meant getResourceAsStream(); getResource returns a URL,
> not an InputStream.

Yes.

Not:

.getResource()

but:

.getResourceAsStream()

or:

.getResource().openStream()

Arne

[toc] | [prev] | [next] | [standalone]


#36065

FromSilvio <silvio@internet.com>
Date2016-12-07 22:06 +0100
Message-ID<584879ec$0$1780$e4fe514c@newszilla.xs4all.nl>
In reply to#36056
On 12/06/2016 09:41 PM, triciac.1999.p@gmail.com wrote:
> I am writing a program that has sound files with type names and numbers like, "win1.wav" and "lose3.wav". I want the program to get the name and path of the running jar file so I can programmatically choose a random sound file of a specified type that is packaged inside the jar file without having to hardcode it myself. Everything I've tried works in Eclipse, but not after it is exported because it doesn't find the name of the jar file, only the path. Is there a way I can get the name AND path of the jar file? Could I search through the jar file packages without using the name and path?
>

There are far better ways to access (sound or any) resources inside the 
JAR you're in than accessing it from the outside. Look at 
ClassLoader#getResources etc.

[toc] | [prev] | [next] | [standalone]


#36655

Fromlingmaaki@gmail.com
Date2017-05-22 23:22 -0700
Message-ID<2ec2a263-945f-41c1-a02e-b941c4fb4d4b@googlegroups.com>
In reply to#36056
 
String jarPath = Test.class.getProtectionDomain().getCodeSource().getLocation().getPath();

String decodedPath = URLDecoder.decode(jarPath, "UTF-8");

http://net-informations.com/java/cjava/default.htm

Ling

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web