absolute path to current application

  • Thread starter Francesco Gallarotti
  • Start date
F

Francesco Gallarotti

I am coding in Java on Mac OS X 10.3.
I use XCode as developement environment.
Is there a way to get the absolute path of the current running application?
 
A

Adam

Francesco Gallarotti said:
I am coding in Java on Mac OS X 10.3.
I use XCode as developement environment.
Is there a way to get the absolute path of the current running
application?

Currently running aplication is JVM and you probably
don't want to get its dir. I guess you are interested
from which directory user started JVM and
his main class.
Try
String currDir = new File(".").getAbsolutePath();

Adam
 
A

Andrew Thompson

I am coding in Java on Mac OS X 10.3.
I use XCode as developement environment.
Is there a way to get the absolute path of the current running application?

Why?

The problem is usually solved
other ways, but it depends on
what you want to do.
 
F

Francesco Gallarotti

I will have my program.app in one folder and all the data i need to run that
program in subfolders of that specific folder. What I need to know is what
is the absolute path of the folder where program.app is....
 
F

Francesco Gallarotti

Thanks, I'll try that!!!


Adam said:
application?

Currently running aplication is JVM and you probably
don't want to get its dir. I guess you are interested
from which directory user started JVM and
his main class.
Try
String currDir = new File(".").getAbsolutePath();

Adam
 
A

Andrew Thompson

I will have my program.app in one folder and all the data i need to run that
program in subfolders of that specific folder. What I need to know is what
is the absolute path of the folder where program.app is....

Class.getResource("TheTopFolderNameOfOptionsSubFolder");

...should get you an URL to the directory then.
Note that very long name only needs to be
the names of the folder, not it's entire path.

A better wat to do this is..
Use system properties class to place a single
String in the system area. That string contains
the path to your app.

Note also that using things like "user.dir"
are prone to failure in arcane circumstances,
whereas either of the above two methods should
work reliably.

HTH
 
J

Jon A. Cruz

Leon said:
You might try.
System.getProperty("user.dir");

In general that gets the current working directory, and can be different
if the user invokes the program from the command-line

The normal way to access program resources is via Class.getResource()
and Class.getResourceAsStream().
 
N

nos

Jon A. Cruz said:
In general that gets the current working directory, and can be different
if the user invokes the program from the command-line

The normal way to access program resources is via Class.getResource()
and Class.getResourceAsStream().

Suppose you run a java program where the class
containing 'main' is in directory abc and another class
that is loaded and used is in another directory, xyz
then what is the answer you seek?
 
D

Dale King

Francesco Gallarotti said:
I will have my program.app in one folder and all the data i need to run that
program in subfolders of that specific folder. What I need to know is what
is the absolute path of the folder where program.app is....

No, what you need is access to the data to run the app. One way to design it
is to put it in folders with the app and try to get the path to where the
app is (which doesn't have any real meaning in Java).

If the data is not data that ever changes for you app, then the correct
approach is to package the data with the app in the jar file and use
getResource to access it.
 
J

Jon A. Cruz

nos said:
Suppose you run a java program where the class
containing 'main' is in directory abc and another class
that is loaded and used is in another directory, xyz
then what is the answer you seek?

if xyz is a subdirectory of abc, then this

getClass().getResource("xyz/foofile");

If it's in a completly different location, then you have a huge
configuration problem on your hands.
 

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
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top