Program Path

  • Thread starter Noé R. Barranco
  • Start date
N

Noé R. Barranco

Hi,
Is there a way to determine the directory where a program is running from ?
I tried "getCanonicalPath()", "getAbsolutePath()", "java.class.path" and
"user.dir" but they return the directory where I'm invoking the application
from, not the directory where it is stored.

Thanks,

--noe
 
M

Michael Borgwardt

Noé R. Barranco said:
Is there a way to determine the directory where a program is running from ?
I tried "getCanonicalPath()", "getAbsolutePath()", "java.class.path" and
"user.dir" but they return the directory where I'm invoking the application
from, not the directory where it is stored.

Well, if that is not "the directory where a program is running from", what is?
The program's classes are not necessarily stored in a single directory, or
even on your computer. Therefore, it makes no sense to ask for
"the directory where it is stored".

I suspect what you really need is ClassLoader.getResourceAsStream(), which
takes into account the above factors automatically.
 
N

Noé R. Barranco

Ok, here's the scenario:
My app is stored on the following directory "d:\apps". If I invoke it from
C:\ (for example), I would like to receive "d:\apps" instead of "C:\".
Hope this clarifies a bit...

Thanks Michael

--noe


Noé R. Barranco said:
Is there a way to determine the directory where a program is running from ?
I tried "getCanonicalPath()", "getAbsolutePath()", "java.class.path" and
"user.dir" but they return the directory where I'm invoking the application
from, not the directory where it is stored.

Well, if that is not "the directory where a program is running from", what
is?
The program's classes are not necessarily stored in a single directory, or
even on your computer. Therefore, it makes no sense to ask for
"the directory where it is stored".

I suspect what you really need is ClassLoader.getResourceAsStream(), which
takes into account the above factors automatically.
 
A

Andy Fish

As Michael said, a java application is just made up of a load of classes
which could come from all over the place, so there is no concept of a single
location for your application.

take a look at class.getResource() and ClassLoader.getResource() which will
help you track down the location on disk that a specific class has been
loaded from
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top