Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Getting a Handle on the Execution Directory

0.00/5 (No votes)
15 Dec 2002 2  
Returns a File and String object that point to the directory that a class is executing from

Introduction

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.

Background

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;
04      
05      //use localDir's getLocalDirRef to make a File Obj

06      myFileRef = myLd.getLocalDirRef();  
07      
08      try
09      {
10         myFr = new FileReader(myFileRef.getPath() + "\\localDir.java");
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;
03       
04      try
05      {
06         myFr = new FileReader(myLd.getLocalDirName() + "\\localDir.java");
07         myBr = new BufferedReader(myFr);
08      }

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

History

Initial release December 16, 2002

Edit History

19 Dec 2002 - Initial Edit

License

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