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. Advertisements

  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. Advertisements

  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. Advertisements

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. Andrew Rowland

    Reg exp: matching relative path only.

    Andrew Rowland, Aug 2, 2003, in forum: Perl
    Replies:
    0
    Views:
    1,224
    Andrew Rowland
    Aug 2, 2003
  2. Matt
    Replies:
    3
    Views:
    20,681
    Sudsy
    Jul 8, 2004
  3. Jamie Green

    Finding a set of Relative Nodes

    Jamie Green, Feb 27, 2004, in forum: XML
    Replies:
    3
    Views:
    529
    Jamie Green
    Feb 29, 2004
  4. Thomas Guettler
    Replies:
    3
    Views:
    988
    Andrei
    Oct 27, 2003
  5. Suresh Jeevanandam

    Finding the relative path of a file from a dir

    Suresh Jeevanandam, Jan 20, 2006, in forum: Python
    Replies:
    4
    Views:
    359
    Fuzzyman
    Jan 20, 2006
  6. Oren
    Replies:
    1
    Views:
    685
    Michael D. Ober
    Apr 29, 2007
  7. Nigel Wilkinson
    Replies:
    2
    Views:
    600
    Nigel Wilkinson
    Jul 25, 2005
  8. Lourens-Jan Ugen

    Finding Relative Maxima in Python3

    Lourens-Jan Ugen, May 31, 2013, in forum: Python
    Replies:
    1
    Views:
    241
    Chris Rebert
    May 31, 2013
Loading...