🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #7621

Re: Applicatio.Path

#7621
From: John McGrath TeamB
Date: Tue, 09 Jan 2001 07:34
47 lines
2849 bytes
On Mon, 8 Jan 2001 22:28:42 +0100, Cheetah wrote:

> I want to retrieve the current path of my application
> In C++ Builder the following line did the job:
> 
> MyApplicationPath = Application->Path;
> 
> Is there something similiar in JBuilder? (Swing)

If you want to make a few assumptions about how your program is being 
loaded, it is possible to write some code to figure this out.  But 
since any such code must depend on several assumptions, there is no 
built-in mechanism to do it for you.

The kind of assumption I am talking about is that your code is being 
loaded from class files or from a JAR file.  While this is extremely 
common, Java does not require it.  Your code could be part of an Applet 
loaded using the HTTP or FTP protocol, or some other protocol that you 
have never heard of.  It could be in an encrypted file attached to an 
executable.  Or it could even be generated on the fly by some unknown 
code.

But pedantry aside, if your code is loaded from class files or from a 
JAR file, you can get a URL to any "file" or resource in your code 
using the Class getResource() method.  In the usual case, your class 
will reside in a "file" based on the class name, so you can use it as 
the "file", using the following:

    URL url = MyClass.class.getResource( "MyClass.class" );

This will return a URL to the class file.  It will be in one of two 
forms, depending on whether your program is loaded from class files or 
a JAR file.  You should be able to use the methods of the File and URL 
classes to extract the part you are looking for.  Make sure you test it 
in both cases, since the format will be very different.

Also note, that this method will not work with Java 1.1, as the format 
of the URL returned is very different.

-- 
Regards,

John McGrath [TeamB]

---------------------------------------------------
Before sending me e-mail, please read:
    http://www.JPMcGrath.net/newsgroups/e-mail.html
Technical Details
Message-ID: <MPG.14c4835b48f6e80d989e5d@forums.borland.com>
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!supernews.com!news.cs.utwente.nl!newshunter!cosy.sbg.ac.at!news.tele.dk!4.1.16.34!cpk-news-hub1.bbnplanet.com!news.gtei.net!news.maxwell.syr.edu!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!bornews.borland.com!not-for-mail
References: <MPG.14c44f7311a2d269989685@forums.inprise.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,2288b62ea4c95126
X-Google-Attributes: gid111d45,public
X-Google-ArrivalTime: 2001-01-08 23:39:18 PST
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!supernews.com!news.cs.utwente.nl!newshunter!cosy.sbg.ac.at!news.tele.dk!4.1.16.34!cpk-news-hub1.bbnplanet.com!news.gtei.net!news.maxwell.syr.edu!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!bornews.borland.com!not-for-mail
From: John McGrath  [TeamB] <ng-b@JPMcGrath.net>
Newsgroups: borland.public.jbuilder.java.swing
Subject: Re: Applicatio.Path
Date: Tue, 9 Jan 2001 02:34:34 -0500
Organization: Another Netscape Collabra Server User
Lines: 47
Message-ID: <MPG.14c4835b48f6e80d989e5d@forums.borland.com>
References: <MPG.14c44f7311a2d269989685@forums.inprise.com>
NNTP-Posting-Host: dslcv1-60.fast.net
X-Newsreader: MicroPlanet Gravity v2.30