Get the directory of the entry file

A

Allan Bruce

Is it possible to get the directory of the file that contains my main()? I
have a few data files in a subdirectory within this directory and I want to
access them like this:

public static final String myDataSubdir = "Data\\";
public static final String myDataFile = "Data1.txt";

// access the file using the below as my String
// myMainDirectory + myDataSubdir + myDataFile


Thanks
Allan
 
A

Andrew Thompson

Is it possible to get the directory of the file that contains my main()?

You do not need to.
<http://www.physci.org/codes/javafaq.jsp#path>
Use Class.getResource() instead. E.G.

<sscce>
import java.net.URL;

/** An example to find resources using Class.getResource(). */
public class FindResource {
public static void main(String args[]) throws Exception {
URL resourceURL;
if ( args.length>0 ) {
Object o = new Object();
resourceURL = o.getClass().getResource(args[0]);
System.out.println( "Name/URL: " + resourceName +
" \t/" + resourceURL );
}
}
}
</sscce>
<op>
C:\FindResource>java FindResource /eg/the.bat
Resource - name/URL:/eg/the.bat /file:/C:/FindResource/eg/the.bat

C:\FindResource>java FindResource eg/the.bat
Resource - name/URL:eg/the.bat /null

C:\FindResource>java FindResource the.bat
Resource - name/URL:the.bat /null
</op>

If the above fails, it might require..
C:\FindResource>java -cp . FindResource /eg/the.bat

You might also consider posting to a different
group at least until you become comfortable with
packages and finding (basic) stuff like this..
<http://www.physci.org/codes/javafaq.jsp#cljh>
 
C

Chris Smith

Allan said:
Is it possible to get the directory of the file that contains my main()? I
have a few data files in a subdirectory within this directory and I want to
access them like this:

public static final String myDataSubdir = "Data\\";
public static final String myDataFile = "Data1.txt";

// access the file using the below as my String
// myMainDirectory + myDataSubdir + myDataFile

If you want to read the file, the easiest way is to use a resource:

URL resource = MyMainClass.class.getResource("data/data1.txt");
// Read the URL using openStream

If you need full read/write access, then things get more complicated.
You should first realize that Java is designed to load classes from
anywhere; including read-only resources like HTTP, hard-to-write
locations like a JAR file (which requires rewriting the entire archive
to make general-case modifications), or potentially even auto-generated
code from an intelligent classloader that isn't stored anywhere at all.

Now if you're absolutely certain that you will always be running your
code from class files stored directly in the filesystem (generally an
unnecessarily messy arrangement), then the following code will probably
work for you:

CodeSource source = MyMainClass.class
.getProtectionDomain().getCodeSource();

if (source == null) return null;

File baseDir;

try
{
URI sourceURI = new URI(source.getLocation().toString());
baseDir = new File(sourceURI);
}
catch (URISyntaxException e)
{
return null;
}
catch (IllegalArgumentException e)
{
return null;
}

if (!baseDir.isDirectory()) return null;

String[] classComponents = MyMainClass.class
.getName().split("\\.");

for (int i = 0; i < classComponents.length - 1; i++)
{
baseDir = new File(baseDir, classComponents);
}

File dataDir = new File(baseDir, "data");
File dataFile = new File(dataDir, "data1.txt");

if (!dataFile.isFile()) return null;
else return dataFile;

HTH,

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
R

Roedy Green

Is it possible to get the directory of the file that contains my main()? I
have a few data files in a subdirectory within this directory and I want to
access them like this:

files that live is the same place as you class files are called
"resources". You can get at them with getResource and
getResourceAsStream.

See http://mindprod.com/jgloss/image.html for sample code.

That is the third time I have typed that today. Why is this suddenly a
hot question?
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top