Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > how to get the path of my current class that running?

Reply
Thread Tools

how to get the path of my current class that running?

 
 
James
Guest
Posts: n/a
 
      10-20-2003
anyone can tell me how to get back the path of my current class that runnig?


 
Reply With Quote
 
 
 
 
Paul Lutus
Guest
Posts: n/a
 
      10-20-2003
James wrote:

> anyone can tell me how to get back the path of my current class that
> runnig?


FIrst, define your terms. A running application may consist of several, or
several dozen, classes, each in principle located in different places.
Therefore you need to say exactly what you want, and why.

--
Paul Lutus
http://www.arachnoid.com

 
Reply With Quote
 
 
 
 
Robert Olofsson
Guest
Posts: n/a
 
      10-20-2003
James ((E-Mail Removed)) wrote:
: anyone can tell me how to get back the path of my current class that runnig?

Basically you can not do that. The current class may come from a class
loader that generates classes by computation only, what would be the
path of such classes.

Why do you need to know this? If you can give a good answear to that
you may get better answears.

It is possible to write a class that uses the class path and search
each entry (directory or jar-file) for the class you are in, that is
quite easy, but it may fail on some classes as stated above.

/robo
 
Reply With Quote
 
John
Guest
Posts: n/a
 
      10-20-2003
I'm not sure about the original poster, but I am new to Java, and I have
wanted to do this on two separate occasion. In both cases I wanted to
reference files from the relative directory location of my classes. In the
first, I wanted to read a configuration file located in WEB-INF. My JAR was
located in WEB-INF\lib. I had to resort to passing a parameter to the
servlet, specifying the path to the config file. This complicates deployment
of my servlet.
In the other case, I had JUnit tests that loaded test data files. I wanted
to use a relative directory path to allow anyone to do a get from the root
of my project in version control, and be able to execute the tests without
setting up their environment. I resorted to defining a single static field
in my test class which defines the absolute directory location. This is not
ideal, since everyone has to change this for their environment.

"Robert Olofsson" <(E-Mail Removed)> wrote in message
news:bn0eqa$p41$(E-Mail Removed)...
> James ((E-Mail Removed)) wrote:
> : anyone can tell me how to get back the path of my current class that

runnig?
>
> Basically you can not do that. The current class may come from a class
> loader that generates classes by computation only, what would be the
> path of such classes.
>
> Why do you need to know this? If you can give a good answear to that
> you may get better answears.
>
> It is possible to write a class that uses the class path and search
> each entry (directory or jar-file) for the class you are in, that is
> quite easy, but it may fail on some classes as stated above.
>
> /robo



 
Reply With Quote
 
Chris Riesbeck
Guest
Posts: n/a
 
      10-20-2003
"John" <(E-Mail Removed)> wrote in message news:<ZjQkb.836720$uu5.147832@sccrnsc04>...
> I'm not sure about the original poster, but I am new to Java, and I have
> wanted to do this on two separate occasion. In both cases I wanted to
> reference files from the relative directory location of my classes. In the
> first, I wanted to read a configuration file located in WEB-INF. My JAR was
> located in WEB-INF\lib. I had to resort to passing a parameter to the
> servlet, specifying the path to the config file.


For this case, use getServletContext().getRealPath(relativePath)

> In the other case, I had JUnit tests that loaded test data files. I wanted
> to use a relative directory path to allow anyone to do a get from the root
> of my project in version control, and be able to execute the tests without
> setting up their environment.


My most recent kludge for this case is

URL dirUrl = MyTestClass.class.getResource("./"); // get my
directory
URL fileUrl = new URL(dirUrl, "../../data.txt"); // get a related
file
String path = fileUrl.getPath().replaceAll("%20", " "); // fix
escaped spaces

All of these steps are suspect so you want to test the results, e.g.,
with File(...).exists(), before running any code and have a backup
plan, e.g., print a message saying "sorry, you'll need to set the test
directory manually."

Not something for external users but perhaps OK as a time saver for
people configuring and testing your app.
 
Reply With Quote
 
Phil...
Guest
Posts: n/a
 
      10-22-2003
What happens if say I am sitting in directory /usr/phil when I run
the program (that happens to live in /usr/bill)
via the console window "java ../bill/whereami"
what would be the result of getResource("./");
would it be /usr/phil or /usr/bill

"Chris Riesbeck" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> "John" <(E-Mail Removed)> wrote in message

news:<ZjQkb.836720$uu5.147832@sccrnsc04>...
> > I'm not sure about the original poster, but I am new to Java, and I have
> > wanted to do this on two separate occasion. In both cases I wanted to
> > reference files from the relative directory location of my classes. In

the
> > first, I wanted to read a configuration file located in WEB-INF. My JAR

was
> > located in WEB-INF\lib. I had to resort to passing a parameter to the
> > servlet, specifying the path to the config file.

>
> For this case, use getServletContext().getRealPath(relativePath)
>
> > In the other case, I had JUnit tests that loaded test data files. I

wanted
> > to use a relative directory path to allow anyone to do a get from the

root
> > of my project in version control, and be able to execute the tests

without
> > setting up their environment.

>
> My most recent kludge for this case is
>
> URL dirUrl = MyTestClass.class.getResource("./"); // get my
> directory
> URL fileUrl = new URL(dirUrl, "../../data.txt"); // get a related
> file
> String path = fileUrl.getPath().replaceAll("%20", " "); // fix
> escaped spaces
>
> All of these steps are suspect so you want to test the results, e.g.,
> with File(...).exists(), before running any code and have a backup
> plan, e.g., print a message saying "sorry, you'll need to set the test
> directory manually."
>
> Not something for external users but perhaps OK as a time saver for
> people configuring and testing your app.



 
Reply With Quote
 
Chris Riesbeck
Guest
Posts: n/a
 
      10-22-2003
"Phil..." <(E-Mail Removed)> wrote in message news:<ksmlb.516488$(E-Mail Removed) .net>...
> What happens if say I am sitting in directory /usr/phil when I run
> the program (that happens to live in /usr/bill)
> via the console window "java ../bill/whereami"
> what would be the result of getResource("./");
> would it be /usr/phil or /usr/bill
>
> "Chris Riesbeck" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> >
> > My most recent kludge for this case is
> >
> > URL dirUrl = MyTestClass.class.getResource("./"); // get my
> > directory
> > URL fileUrl = new URL(dirUrl, "../../data.txt"); // get a related
> > file
> > String path = fileUrl.getPath().replaceAll("%20", " "); // fix
> > escaped spaces


Where you're sitting shouldn't make any difference.
MyTestClass.class getResource() returns a URL based on where
MyTestClass is sitting.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get Full Path of current directory ? (ASP.net) PHaroZ ASP .Net 5 08-17-2012 08:39 AM
How to get current folder path? Chung ASP .Net 2 01-20-2008 02:09 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
How to get access to current page class from other class? John Smith ASP .Net 9 03-11-2005 03:57 PM
Current Class Path RB Java 3 10-29-2003 05:56 PM



Advertisments