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: Tue, 6 Dec 2016 21:18:42 -0500 Organization: Aioe.org NNTP Server Lines: 32 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: 7bit 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:36062 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