How to get the absolute path?

Discussion in 'Java' started by John, Aug 1, 2004.

  1. John

    John Guest

    Hi.

    I wrote the codes bellow.

    <example>

    public class TestApp {

    public static void main(String[] args) {

    // Question : How to get the absolute path of "this" ?

    }
    }

    </example>

    Thank for your answer to the question above.
     
    John, Aug 1, 2004
    #1
    1. Advertising

  2. John

    Morten Alver Guest

    > public class TestApp {
    >
    > public static void main(String[] args) {
    >
    > // Question : How to get the absolute path of "this" ?
    >
    > }
    > }


    What do you mean by 'the absolute path of "this"'? If you mean the
    current working directory, you can use System.getProperty(). See this page:

    http://java.sun.com/docs/books/tutorial/essential/system/properties.html


    --
    Morten
     
    Morten Alver, Aug 2, 2004
    #2
    1. Advertising

  3. John

    John Guest

    "Morten Alver" <morten@no_spam.boblefisk.org> wrote in message
    news:410d6936$...
    > > public class TestApp {
    > >
    > > public static void main(String[] args) {
    > >
    > > // Question : How to get the absolute path of "this" ?
    > >
    > > }
    > > }

    >
    > What do you mean by 'the absolute path of "this"'? If you mean the
    > current working directory, you can use System.getProperty(). See this

    page:
    >
    > http://java.sun.com/docs/books/tutorial/essential/system/properties.html
    >


    Thank you for your reply.
    "this" means the absolute path of the file "TestApp.class".
    System.getProperty("user.dir") don't return the path.
    For example ( I use the Eclipse platform),

    <example>

    <directory>

    c:\eclipse
    | - workspace
    | - TestAppProject
    | - TestAppPackage
    | -
    TestApp.class
    | -
    TestApp.java


    </directory>

    <codes>

    public class TestApp {

    public static void main(String[] args) {

    String path = System.getProperty("user.dir");
    System.out.print(path);
    }
    }

    </codes>

    </example>

    In the example above, System.getProperty() returns the path
    "c:\eclipse\workspace\TestAppProject".
    Is there the easy and simple way to get the absolute path of the file
    "TestApp.class" ?

    Thanks.
     
    John, Aug 2, 2004
    #3
  4. John

    hilz Guest

    "John" <> wrote in message news:cej8ui$gmc$...
    > Hi.
    >
    > I wrote the codes bellow.
    >
    > <example>
    >
    > public class TestApp {
    >
    > public static void main(String[] args) {
    >
    > // Question : How to get the absolute path of "this" ?
    >
    > }
    > }
    >
    > </example>
    >
    > Thank for your answer to the question above.
    >
    >



    could it be this:

    String getAbsolutePath(){
    java.security.ProtectionDomain pd =
    YourClassName.class.getProtectionDomain();
    if ( pd == null ) return null;
    java.security.CodeSource cs = pd.getCodeSource();
    if ( cs == null ) return null;
    java.net.URL url = cs.getLocation();
    if ( url == null ) return null;
    java.io.File f = new File( url.getFile() );
    if (f == null) return null;

    return f.getAbsolutePath();
    }
     
    hilz, Aug 2, 2004
    #4
  5. "John" <> writes:

    > "this" means the absolute path of the file "TestApp.class".


    That would be the URL returned by
    TestApp.class.getResource("TestApp.class");
     
    Tor Iver Wilhelmsen, Aug 2, 2004
    #5
  6. John

    mukeshkoshym

    Joined:
    Apr 5, 2011
    Messages:
    1
    This really works. Thanks for the information.
     
    mukeshkoshym, Apr 5, 2011
    #6
    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,631
    Sudsy
    Jul 8, 2004
  2. Replies:
    10
    Views:
    3,681
  3. Thomas Guettler
    Replies:
    3
    Views:
    774
    Andrei
    Oct 27, 2003
  4. Nigel Wilkinson
    Replies:
    2
    Views:
    401
    Nigel Wilkinson
    Jul 25, 2005
  5. James Byrne
    Replies:
    3
    Views:
    581
    James Byrne
    Sep 14, 2010
Loading...

Share This Page