finding an relative path

Z

ziv

can you please help me how to find the relative path from where the
main class is running.
for example ifwe run the main class from c:\ so this function will
return the string associated with the path.
thanx
 
M

Miguel De Anda

Roedy Green said:
That is called the current directory. See
http://mindprod.com/jgloss/file.html

If you want the working directory it works fine but if you want to know
where the "main class is running" from (where MainClass.class) is at then it
won't work. I even tried it.

import java.io.*;

public class WorkingDir {
public static void main(String[] args) throws Exception {
File f = new File(".");
String path = f.getCanonicalPath();
System.out.println(path);
}
}

If I have the class file in a folder "WD/a/" and run this command from "WD/"

java -cp a WorkingDir

I get WD

Where if I run this command from "WD/a/"

java WorkingDir

I get WD/a


I found a small library somewhere once that managed to get that information.
Of course I can't find it now, but if you search the web you will find it.
In fact, it claims to get the directory where the jar file is if it is
running from a jar file.
 
T

Tor Iver Wilhelmsen

Miguel De Anda said:
If you want the working directory it works fine but if you want to know
where the "main class is running" from (where MainClass.class) is at then it
won't work. I even tried it.

What you can try is asking the ClassLoader for the resource
"your/package/and.class". You will get an URL you can examine.
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top