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

P

Paul Lutus

James said:
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.
 
R

Robert Olofsson

James ([email protected]) 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
 
J

John

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.
 
C

Chris Riesbeck

John said:
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.
 
P

Phil...

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
 
C

Chris Riesbeck

Phil... said:
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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top