finding an relative path

Discussion in 'Java' started by ziv, Aug 19, 2003.

  1. ziv

    ziv Guest

    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
     
    ziv, Aug 19, 2003
    #1
    1. Advertising

  2. ziv

    Roedy Green Guest

    On 19 Aug 2003 14:34:04 -0700, (ziv) wrote or
    quoted :

    >can you please help me how to find the relative path from where the
    >main class is running.


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

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Aug 19, 2003
    #2
    1. Advertising

  3. "Roedy Green" <> wrote in message
    news:...
    > On 19 Aug 2003 14:34:04 -0700, (ziv) wrote or
    > quoted :
    >
    > >can you please help me how to find the relative path from where the
    > >main class is running.

    >
    > That is called the current directory. See
    > http://mindprod.com/jgloss/file.html
    >
    > --
    > Canadian Mind Products, Roedy Green.
    > Coaching, problem solving, economical contract programming.
    > See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.


    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.
     
    Miguel De Anda, Aug 20, 2003
    #3
  4. "Miguel De Anda" <_sodamnmad_@_hotmail_._com_> writes:

    > 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.
     
    Tor Iver Wilhelmsen, Aug 20, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    3
    Views:
    19,625
    Sudsy
    Jul 8, 2004
  2. Thomas Guettler
    Replies:
    3
    Views:
    772
    Andrei
    Oct 27, 2003
  3. Suresh Jeevanandam

    Finding the relative path of a file from a dir

    Suresh Jeevanandam, Jan 20, 2006, in forum: Python
    Replies:
    4
    Views:
    310
    Fuzzyman
    Jan 20, 2006
  4. Oren
    Replies:
    1
    Views:
    558
    Michael D. Ober
    Apr 29, 2007
  5. Nigel Wilkinson
    Replies:
    2
    Views:
    399
    Nigel Wilkinson
    Jul 25, 2005
Loading...

Share This Page