Click here to Skip to main content
13,297,266 members (61,529 online)
Click here to Skip to main content
Add your own
alternative version


11 bookmarked
Posted 15 Dec 2002

Getting a Handle on the Execution Directory

, 15 Dec 2002
Rate this:
Please Sign up or sign in to vote.
Returns a File and String object that point to the directory that a class is executing from
<!-- Download Links --> <!-- Add the rest of your HTML here -->


localDir will return a String and File reference to the directory that your code is executing in. This makes it possible to easily reference disk files that exist in the same directory that you are running your java class from.


I guess I've been a bit polluted by programming in C and C++ in the Win32 world. I grew to expect opening files without any directory reference meant that the file I wanted to open was in the same directory that my source and executable code are in. Java defaults to the bin directory of the JRE installation if you don't specify a path when opening files.

Java provides some handy, and very portable, path references with the System.getProperties() method. With this call I can get my temp, home, user, library, and class paths.

For small applications and for teaching purposes I wanted my source, executables and data files all in the same directory. With some help from the Java newsgroups and some String parsing, I figured out how to do it.

Using the code

Instantiate an object of type localDir

localDir myLd = new localDir();

To get (and print) the name of the class disk file name:

System.out.println("Class Name = " + myLd.getClassName());

This is not really useful, other than to see what file is being used to determine the local directory. It will always return localDir.class as this is the class that is being executed when you call getClassName()

The real fun is when you call the getLocalDirRef() method

01      File myFileRef = null;
02      BufferedReader myBr = null;
03      FileReader myFr = null;
05      //use localDir's getLocalDirRef to make a File Obj
06      myFileRef = myLd.getLocalDirRef();  
08      try
09      {
10         myFr = new FileReader(myFileRef.getPath() + "\\");
11         myBr = new BufferedReader(myFr);
12      }

Now we can get a File object reference to our local directory. Line 06 calls the method and gives up the desired path and directory that we are executing in. It returns as a File object, so have one ready to hold the info.

Line 10 uses the newly acquired info and calls the File's getPath() method to divulge the name of the path. Finally, the desired file name is concatenated to the path and I use it to open a FileReader.

It might make more sense to use the localDir's getLocalDirName() method to just get a String variable to hold the path and directory name in the above code, and you are right.

01      File myFileRef = null;
02      BufferedReader myBr = null;
04      try
05      {
06         myFr = new FileReader(myLd.getLocalDirName() + "\\");
07         myBr = new BufferedReader(myFr);
08      }

Short and sweet, but the File object reference is still there if you need it.


Initial release December 16, 2002

Edit History

19 Dec 2002 - Initial Edit


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Mark Kozel
Web Developer
United States United States
I grew up in California. Attended Cal Poly, San Luis Obispo and left with a BS in Computer Science and a Student Loan to pay off.

I've been exposed to Basic, Pasal, ADA, Java, C, and C++. I'm currently working in the real-time military world (very cool) and teaching Java (even cooler)!

You may also be interested in...


Comments and Discussions

GeneralSimpler implementation of getClassName() Pin
Mike Monteiro16-Oct-08 4:38
memberMike Monteiro16-Oct-08 4:38 
GeneralStatics... Pin
Davy Boy24-May-05 4:14
memberDavy Boy24-May-05 4:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 16 Dec 2002
Article Copyright 2002 by Mark Kozel
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid