Getting the absolute path of the current executing file

I have a file which will be placed in window and linux environment. It is not good to change the source code in that way:

String path = "D:\\java\file1.txt"; (Window)

String path = "/java/file1.txt"; (Linux)

So I would like to ask how to get the absolute path based on that executing file?

I referred to the reference in jsp, but I don't know what class and the coding syntax in console environment.

Thx for any help.

[455 byte] By [roamera] at [2007-11-27 7:58:33]
# 1

If you are looking to "get" a file that is located in a directory with (or somewhere under) the class file than use

myClass.getClass().getResource(<relativePathWithFileFromClass>); //URL

myClass.getClass().getResourceAsStream(<relativePathWithFileFromClass>); //InputStream

As far as determining which Operating System you are on, there are a number of environment variables that will tell you that, and you can then format your file path accordingly.

You can use "/" in your path regardless of which OS you are using. You do not have to use "\\" on Windows (excpet maybe inside of a Runtime.exec command string).

Here is a very small program you can compile and run if you wish to see the list of System Properties available:

public class ShowProperties {

public static void main(String[] args) {

System.getProperties().list(System.out);

}

}

Just save that to a file (named ShowProperties.java of course) and compile and run it, and you will get a list of your available System Properties and their current values

masijade.a at 2007-7-12 19:40:30 > top of Java-index,Java Essentials,Java Programming...
# 2
Get the path separator from java. The system dependant name separator char.File.separator
haishaia at 2007-7-12 19:40:30 > top of Java-index,Java Essentials,Java Programming...
# 3

public class t4{

public static void main(String args[]){

t4 obj1 = new t4();

String tmp1 = obj1.getClass().getResource("/"); //URL

String tmp2 = obj1.getClass().getResourceAsStream("/"); //InputStream

}

}

t4.java:11: incompatible types

found: java.net.URL

required: java.lang.String

String tmp1 = obj1.getClass().getResource("/"); //URL

^

t4.java:12: incompatible types

found: java.io.InputStream

required: java.lang.String

String tmp2 = obj1.getClass().getResourceAsStream("/"); //InputStream

^

2 errors

I tried it but got the following error.....can you give me some simple example?

roamera at 2007-7-12 19:40:30 > top of Java-index,Java Essentials,Java Programming...
# 4
You've made an assumption which is not correct.1. Have a look at the comments in the code you were given.2. Look up the APIs.
ejpa at 2007-7-12 19:40:30 > top of Java-index,Java Essentials,Java Programming...
# 5
As the comments say, the first one returns a URL, which can be plugged directly into a File constructor, and the second one returns an InputStream ready to be read from.Please refer to the API.
masijade.a at 2007-7-12 19:40:30 > top of Java-index,Java Essentials,Java Programming...
# 6
> the first one returns a URL, which can be plugged directly into a File> constructorNo, but you can get an InputStream directly from it ...
ejpa at 2007-7-12 19:40:30 > top of Java-index,Java Essentials,Java Programming...
# 7

> > the first one returns a URL, which can be plugged

> directly into a File

> > constructor

>

> No, but you can get an InputStream directly from it

> ...

Sorry, I meant after using URLs toURI method. The File Constructor takes a URI now. I haven't tried it with a URL received from getResource, but it should work.

masijade.a at 2007-7-12 19:40:30 > top of Java-index,Java Essentials,Java Programming...
# 8
Surely only if it's a file: URI, which it won't be if the resource is in a JAR file, or on a server ...
ejpa at 2007-7-12 19:40:30 > top of Java-index,Java Essentials,Java Programming...
# 9
> Surely only if it's a file: URI, which it won't be if> the resource is in a JAR file, or on a server ...True.;-)
masijade.a at 2007-7-12 19:40:30 > top of Java-index,Java Essentials,Java Programming...
# 10

I found using this method can also get the absolute path based on the current executing file, as below:

import java.io.File;

public class GetPath{

public static void main(String args[]) throws Exception{

File tmpfile = new File("");

System.out.println(tmpfile.getAbsolutePath());

}

}

roamera at 2007-7-12 19:40:30 > top of Java-index,Java Essentials,Java Programming...
# 11
That gives you the current working directory. Not the same thing.
ejpa at 2007-7-12 19:40:30 > top of Java-index,Java Essentials,Java Programming...
# 12

> So I would like to ask how to get the absolute path

> based on that executing file?

It's not possible in the general case.

If you explain why you think you need it and which sources your class can be loaded from, then maybe somebody can suggest a better approach or tell you what will work for your particular situation.

jverda at 2007-7-12 19:40:30 > top of Java-index,Java Essentials,Java Programming...
# 13

I have a java file, which will be placed in a window machine and linux machine separately. They will run this file every midnight, and in my file, I need to write a log file, which is in the ./log/20070620.txt

What I want, is to get the full path of the current location, assign it as a String variable, then use it to form a filepath to write out the log file.

Now, my approach is compile this file twice, hardcoding the full path in this way:

outputStream = new PrintWriter(new FileWriter("D:\\java\\AutoJob\\log\\"+today+".txt"));//For Window

outputStream = new PrintWriter(new FileWriter("/java/AutoJob/log/"+today+".txt")); //For Linux

My purpose is to get the current fullpath which is system specific, so that I won't have to hardcode it.

roamera at 2007-7-12 19:40:30 > top of Java-index,Java Essentials,Java Programming...
# 14

You can "hardcode" the path easily, because, as I have said, you can use "/" in the filepath on all systems (even Windows). So, you need only to decide whether to start with a "D:" or whatever (which is also, at least slightly, irrelevant, as it will , when the path starts with a single "/" use the drive that was "active" when the currently running process was started), and you can get that by looking at the Properties (as I have also mentioned ("os.name" and/or "user.dir" and/or "java.home" maybe)).

You may wish to have the path to the logfile contained in a Properties file, however, or even use Preferences, to allow this to be configurable.

masijade.a at 2007-7-12 19:40:30 > top of Java-index,Java Essentials,Java Programming...
# 15

String currentdir = System.getProperty("user.dir");

String sep = System.getProperty("file.separator");

outputStream = new PrintWriter(new FileWriter(currentdir + sep +"log" + sep +today+".txt"));

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html

Is that right?

roamera at 2007-7-21 22:25:39 > top of Java-index,Java Essentials,Java Programming...
# 16
It'll work, as long as the program is started from the right place.
masijade.a at 2007-7-21 22:25:39 > top of Java-index,Java Essentials,Java Programming...