🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.api
Article #738

Re: Jar's path at the runtime

#738
From: Shankar Unni
Date: Wed, 04 Dec 2002 18:07
28 lines
2237 bytes
Alexei Akimov wrote:

> I have a following question: Let we have a java application packed into jar
> file or native executable (using JBuilder). Can the application determine
> jar or exe file path at the runtime? 

There is a near-exact equivalent of GetModuleName (which gives you the 
executable or  DLL that defines an entry point) : you can find out where 
a particular loaded class is located, as follows (if you have an object 
of that class)

   obj.getClass().getProtectionDomain().getCodeSource().getLocation()

This expression returns you a java.net.URL of where obj.getClass() was 
loaded from.  For instance, when I run this in a standalone class file 
in the current directory, I get:

   file:/C:/Shankar/

which means that my "bar.class" was loaded from this directory (it's a 
given that "class bar" is in a file called "bar.class").  Similarly, if 
I shove bar.class into an "xxx.jar", put that in my classpath and 
execute the same program, I get

   file:/C:/Shankar/xxx.jar

--
Shankar.
Technical Details
Message-ID: <3dee4449$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-03!sn-xit-02!sn-xit-06!sn-xit-04!supernews.com!newshosting.com!news-xfer1.atl.newshosting.com!newspeer.monmouth.com!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.icl.net!newsfeed.fjserv.net!lnewspeer00.lnd.ops.eu.uu.net!emea.uu.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <3deddcdc@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1089d0,734864463187f08
X-Google-Attributes: gid1089d0,public
X-Google-ArrivalTime: 2002-12-04 10:10:09 PST
Date: Wed, 04 Dec 2002 10:07:06 -0800
From: Shankar Unni <shankar@cotagesoft.com>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.2.1) Gecko/20021130
X-Accept-Language: en-us, en
MIME-Version: 1.0
Newsgroups: borland.public.jbuilder.java.api
Subject: Re: Jar's path at the runtime
References: <3deddcdc@newsgroups.borland.com>
In-Reply-To: <3deddcdc@newsgroups.borland.com>
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 64.165.207.59
Message-ID: <3dee4449$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1039025225 64.165.207.59 (4 Dec 2002 10:07:05 -0800)
Lines: 29
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-03!sn-xit-02!sn-xit-06!sn-xit-04!supernews.com!newshosting.com!news-xfer1.atl.newshosting.com!newspeer.monmouth.com!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed.icl.net!newsfeed.fjserv.net!lnewspeer00.lnd.ops.eu.uu.net!emea.uu.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail