Get root directory of application

D

Dave

How do I access the root directory of an application?

Say I want to access a file which is stored in the highest-level directory
of the application. How do I access this with a relative path rather than an
absolute path?
 
T

Thomas Kellerer

How do I access the root directory of an application?

Say I want to access a file which is stored in the highest-level directory
of the application. How do I access this with a relative path rather than an
absolute path?

Swing or Web application ?
 
A

Andrew Thompson

Dave said:
How do I access the root directory of an application?

You 'shouldn't' need to...
Say I want to access a file which is stored in the highest-level directory
of the application. ...

Say instead, ..what are you trying to achieve
by that?

(Don't tell us what you *want* to do, but what
you are trying to *achieve* and you will get
better answers.)

Andrew T.
 
A

Andrew Thompson

John W wrote:

Please refrain from top-posting.
This might work:

String path = (new File (".")).getCanonicalPath();

Only at random. The current directory is rarely the
root directory of real world applications.

Andrew T.
 
D

Dave

Andrew Thompson said:
You 'shouldn't' need to...


Say instead, ..what are you trying to achieve
by that?

(Don't tell us what you *want* to do, but what
you are trying to *achieve* and you will get
better answers.)

I am trying to load a Castor JDO mapping file from a specific directory
within the application structure without using absolute paths.

The DRIVER String final field is "mapping.xml", and it only works when I
have the XML file in the same directory as the compiled class file for this
class. I would like it to be in [app_root]/mapping, but I am unsure how to
represent this as a relative path.

/* Sample code below */

org.castor.jdo.conf.Driver driverConf = JDOConfFactory.createDriver(DRIVER,
CONNECT, USERNAME, PASSWORD);

org.castor.jdo.conf.Mapping mappingConf = null;
try {
mappingConf =
JDOConfFactory.createMapping(getClass().getResource(MAPPING).toString());
} catch (NullPointerException e) {
LOG.error("NullPointerException - JDO XML Mapping file not found.");
e.printStackTrace();
return false;
}


TIA.
 
A

Andrew Thompson

Dave wrote:
...
I am trying to load a Castor JDO mapping file .. (snip)
.. without using absolute paths.

Put it in a jar and add the jar to the classpath,
then use getResource() to locate it. You never
need to worry about the actual path.

Andrew T.
 
D

Dave

Andrew Thompson said:
Dave wrote:
..

Put it in a jar and add the jar to the classpath,
then use getResource() to locate it. You never
need to worry about the actual path.

Sounds good for production, but time consuming when I'm constantly modifying
the file during development.

Thanks.
 
A

Andrew Thompson

Dave said:
...
Sounds good for production, but time consuming when I'm constantly modifying
the file during development.

Use ant for your development needs, and a
build is but a click and a few moments away..

Andrew T.
 
N

Nigel Wade

Dave said:
How do I access the root directory of an application?

Say I want to access a file which is stored in the highest-level directory
of the application. How do I access this with a relative path rather than an
absolute path?

I think this is right. If it's not I'm sure some helpful soul will correct me.

You get the path to the base of the class hierarchy by specifying a resource
which has a root "/", e.g.

URL root = getClass().getResource("/");

For example, if you have a class Xyz.class in the package "foo.bar", so your
class file is located in /some/base/dir/foo/bar/Xyz.class, then the resource
"/resources/filename" would be the path /some/base/dir/resources/filename.
 
T

Thomas Kellerer

Dave wrote on 10.01.2007 16:29:
Sounds good for production, but time consuming when I'm constantly modifying
the file during development.

getResource() works perfectly fine without jars as well. I used it all the time,
and have no problems from within the IDE (NetBeans) or when running my app from
a jar file

Thomas
 
A

Andrew Thompson

Thomas said:
Dave wrote on 10.01.2007 16:29: ....
getResource() works perfectly fine without jars as well. ...

I must remember that. (It has been mentioned
before, and I keep forgetting, mostly because ant
builds seem to make it ..redundant.)

Andrew T.
 
T

Thomas Kellerer

Andrew Thompson wrote on 10.01.2007 18:34:
I must remember that. (It has been mentioned
before, and I keep forgetting, mostly because ant
builds seem to make it ..redundant.)


In my case: NetBeans makes the .jar file redundant ;)
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Dave said:
How do I access the root directory of an application?

Say I want to access a file which is stored in the highest-level directory
of the application. How do I access this with a relative path rather than an
absolute path?

As several has pointed out then you need to define carefully what
is the root of an application.

Regarding finding the path to the code being executed then I have
the code snippet attached below.

Arne

=============================================

private String getPath(Class cls) {
String cn = cls.getName();
StringTokenizer st = new StringTokenizer(cn,".");
StringBuffer sb = new StringBuffer("");
boolean first = true;
while(st.hasMoreTokens()) {
if(first) {
first = false;
} else {
sb.append("/");
}
sb.append(st.nextToken());
}
sb.append(".class");
String path =
getClass().getClassLoader().getResource(sb.toString()).getPath();
int ix = path.indexOf("!");
if(ix >= 0) {
path = path.substring(0, ix);
int ix2 = path.lastIndexOf("/");
return path.substring(6, ix2 + 1);
} else {
int ix2 = path.lastIndexOf("/");
return path.substring(1, ix2 + 1);
}
}
 

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,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top