Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

2,092 views
Skip to first unread message

triciac...@gmail.com

unread,
Dec 6, 2016, 3:42:04 PM12/6/16
to
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?

Eric Sosman

unread,
Dec 6, 2016, 4:07:51 PM12/6/16
to
On 12/6/2016 3:41 PM, triciac...@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.

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

Tricia Cahill

unread,
Dec 6, 2016, 5:23:10 PM12/6/16
to
Thank you. Thank you so much.

Arne Vajhøj

unread,
Dec 6, 2016, 9:18:53 PM12/6/16
to
On 12/6/2016 3:41 PM, triciac...@gmail.com 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 '!'.

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

Silvio

unread,
Dec 7, 2016, 4:07:19 PM12/7/16
to
On 12/06/2016 09:41 PM, triciac...@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.

Nigel Wade

unread,
Dec 8, 2016, 8:20:38 AM12/8/16
to
On 07/12/16 02:18, Arne Vajhøj wrote:
> On 12/6/2016 3:41 PM, triciac...@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.

Wayne

unread,
Dec 9, 2016, 1:50:27 AM12/9/16
to
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

Arne Vajhøj

unread,
Dec 11, 2016, 8:10:09 PM12/11/16
to
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


Arne Vajhøj

unread,
Dec 11, 2016, 8:24:36 PM12/11/16
to
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




Nigel Wade

unread,
Dec 12, 2016, 4:51:45 AM12/12/16
to
It is definitely possible, but it's still good to be aware of the caveats before they cause you problems.

Arne Vajhøj

unread,
Dec 12, 2016, 8:25:23 PM12/12/16
to
Absolutely.

Arne


ling...@gmail.com

unread,
May 23, 2017, 2:22:23 AM5/23/17
to

String jarPath = Test.class.getProtectionDomain().getCodeSource().getLocation().getPath();

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

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

Ling
0 new messages