Results 1 to 9 of 9
  1. #1
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default Command to return the path the class is curently in.

    Hello all.

    This is my first post.

    I need to be able to return the path the class is running from.

    This is what I have come up with:

    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class ReturnPath {
        public static void main( String[] args ) {
            ReturnPath m = new ReturnPath();
            m.getPath();
        }
    
        public void getPath() {
            File f = new File( "" );
            
            String pathName = f.getAbsolutePath();
            
            System.out.println( pathName );
        }
    }
    This appears to work, does everyone agree with what I have done.

  2. #2
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Another way is:
    Java Code:
    System.getProperty("user.dir");
    Last edited by ra4king; 04-29-2011 at 06:29 AM.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default

    That will give the path to the folder/package of that specific class.
    No, it'll give the path of the current working directory.

    If you want the path to the folder/package of the main program class, you would do:
    Java Code:
    System.getProperty("user.dir");
    No, that'll give the path to the user's home directory, which has nothing to do with where the .class file is located.

    db

  4. #4
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    @Darryl.Burke
    No "user.dir" returns the "User's current working directory".
    "user.home" returns the user's home directorr.
    System (Java Platform SE 6))

    But you are quite correct on the first statement. I got confused with something else, the OP's way works too.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Of "user.dir":

    Quote Originally Posted by Darryl.Burke View Post
    No, that'll give the path to the user's home directory, which has nothing to do with where the .class file is located.

    Umm, well, ...

    @OP: See for yourself. Compile and run this all over the place to see what these things give you.

    Java Code:
    import java.io.File;
    
    public class Test {
        public static void main(String[] args) {
            System.out.println(new File("").getAbsolutePath());
            System.out.println(System.getProperty("user.home"));
            System.out.println(System.getProperty("user.dir"));
        }
    }
    [Edit] slow ;( But it is a good idea to try these things out, follow up on the API docs etc.
    Last edited by pbrockway2; 04-29-2011 at 06:36 AM.

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by stchman View Post
    ... I need to be able to return the path the class is running from.
    ..
    Why do you need to do this?

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default

    Quote Originally Posted by ra4king View Post
    @Darryl.Burke
    No "user.dir" returns the "User's current working directory".
    "user.home" returns the user's home directorr.
    Oops, sorry about that.

    db

  8. #8
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Of "user.dir":




    Umm, well, ...

    @OP: See for yourself. Compile and run this all over the place to see what these things give you.

    Java Code:
    import java.io.File;
    
    public class Test {
        public static void main(String[] args) {
            System.out.println(new File("").getAbsolutePath());
            System.out.println(System.getProperty("user.home"));
            System.out.println(System.getProperty("user.dir"));
        }
    }
    [Edit] slow ;( But it is a good idea to try these things out, follow up on the API docs etc.
    I ran the following program:

    PHP Code:
    import java.util.*;
    import java.io.*;
    
    public class ReturnPath {
        public static void main( String[] args ) {
            ReturnPath m = new ReturnPath();
            m.getPath();
        }
    
        public void getPath() {
            File f = new File( "" );
            String pathName = f.getAbsolutePath();
            
            System.out.println( pathName );
            System.out.println( System.getProperty( "user.home" ) );
            System.out.println( System.getProperty( "user.dir" ) );
    
        }
    }
    Here is the output:
    Java Code:
    /media/storage/java_progs
    /home/bob
    /media/storage/java_progs
    Apparently System.getProperty( "user.dir" ) will give me what I want.

    Thanks all.

  9. #9
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

Similar Threads

  1. Finding path to my command line tools from Java
    By paulanon in forum New To Java
    Replies: 1
    Last Post: 04-07-2011, 02:34 AM
  2. How to return command prompt error message
    By raxwer in forum Advanced Java
    Replies: 3
    Last Post: 05-26-2010, 05:15 PM
  3. Replies: 2
    Last Post: 03-31-2010, 12:26 AM
  4. want to set two path in one command
    By Hussain Ali in forum New To Java
    Replies: 1
    Last Post: 02-24-2010, 07:50 AM
  5. setting class-path & Library Path in ubantu
    By programmer_007 in forum Eclipse
    Replies: 18
    Last Post: 02-22-2010, 12:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •