File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Finding base directory of main class file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Finding base directory of main class file" Watch "Finding base directory of main class file" New topic
Author

Finding base directory of main class file

Brian Snyder
Ranch Hand

Joined: Feb 03, 2001
Posts: 142
I know how to find the base directory of an applet.
ie. getDocumentBase();
but how do I determine the base directory or where all the files in a standalone application are located?
The reason I ask is that I have created a Java software program that will be installed on an end user's machine. I need the program to be able to detrmine the base directory like the getDocumentBase() function for an applet above.
It would be nice if they placed it in c:\programName...., but that may not be the case.
They may use d:\someOtherFolder\programName....
Many thanks in advance.
Brian
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
The File class provides several methods for navigating a file system. getParent(), getPath(), getAbsolutePath() ...


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Brian Snyder
Ranch Hand

Joined: Feb 03, 2001
Posts: 142
Thanks Marylin,
I would like to take this a step further. So let's say I create an instance of File.
File file = new File(???);
How do I know the abstract path name to enter if the file could be different depending on the way the user installed my software?
I know it is easy to get the parent directory once a file is known, but what if where it originates (its abstract path) changes with each installation.
ie.
Some people will install as follows .... c:\Program
Some people might install as follows .... d:\Program
Maybe I need to write the starting directory in a properties file so the program can read the pathname into the program upon execution??? If so, how can I record that?
Thanks again for your help.
Brian
[This message has been edited by Brian Snyder (edited October 07, 2001).]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Try this demo
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Finding base directory of main class file