Article View: borland.public.jbuilder.java.api
Article #881Re: How to get a file path?
From: changxh
Date: Sat, 20 Jul 2002 02:47
Date: Sat, 20 Jul 2002 02:47
28 lines
2298 bytes
2298 bytes
How do I find the directory containing my application
public static File getApplicationDirectory( Class clas ) {
ProtectionDomain pd = clas.getProtectionDomain();
if ( pd == null ) return null;
CodeSource cs = pd.getCodeSource();
if ( cs == null ) return null;
URL url = cs.getLocation();
if ( url == null ) return null;
return new File( url.getFile() ).getParentFile();
}
This will return the directory that contains the JAR file that
contains the specified class. If the class is in a directory tree, it will
return the parent of the class root directory, so in a typical JBuilder
configuration (with Classes under the project directory), you will get the
project directory.
Note that it is apparently possible for this to return null under some
circumstances. I have heard of one person who got a null CodeSource, but I
do not know under what circumstances that happens. It has always worked for
me.
JPMcGrath@JPMcGrath.net
[Append to This Answer]
Previous: Launching the default browser
Next: Does JBuilder provide anything for generating reports?
This document is: http://www.visi.com/~gyles19/cgi-bin/fom.cgi?file=391
[Search] [Appearance]
This is a Faq-O-Matic 2.709.
This FAQ administered by gyles19@visi.com.
Technical Details
Message-ID:
<3d38cfa3_2@dnews>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-05!sn-xit-01!sn-xit-04!supernews.com!newsfeed.online.be!fu-berlin.de!news.rhrz.uni-bonn.de!RRZ.Uni-Koeln.DE!nntp.cs.uni-magdeburg.de!PeerFeed.KWeb.on.ca!dnewsReferences:
<3d2a44c6_1@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1089d0,d0d13928dad34149 X-Google-Attributes: gid1089d0,public X-Google-ArrivalTime: 2002-07-19 20:08:12 PST Reply-To: "changxh" <chang_xiaohui@163.com> From: "changxh" <chang_xiaohui@163.com> Newsgroups: borland.public.jbuilder.java.api References: <3d2a44c6_1@dnews> Subject: Re: How to get a file path? Date: Sat, 20 Jul 2002 10:47:02 +0800 Lines: 32 Organization: none X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 NNTP-Posting-Host: 218.17.73.3 Message-ID: <3d38cfa3_2@dnews> X-Trace: dnews 1027133347 218.17.73.3 (19 Jul 2002 19:49:07 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-05!sn-xit-01!sn-xit-04!supernews.com!newsfeed.online.be!fu-berlin.de!news.rhrz.uni-bonn.de!RRZ.Uni-Koeln.DE!nntp.cs.uni-magdeburg.de!PeerFeed.KWeb.on.ca!dnews