how to get the path of my current class that running?

Discussion in 'Java' started by James, Oct 20, 2003.

  1. James

    James Guest

    anyone can tell me how to get back the path of my current class that runnig?
    James, Oct 20, 2003
    #1
    1. Advertising

  2. James

    Paul Lutus Guest

    James wrote:

    > anyone can tell me how to get back the path of my current class that
    > runnig?


    FIrst, define your terms. A running application may consist of several, or
    several dozen, classes, each in principle located in different places.
    Therefore you need to say exactly what you want, and why.

    --
    Paul Lutus
    http://www.arachnoid.com
    Paul Lutus, Oct 20, 2003
    #2
    1. Advertising

  3. James () wrote:
    : anyone can tell me how to get back the path of my current class that runnig?

    Basically you can not do that. The current class may come from a class
    loader that generates classes by computation only, what would be the
    path of such classes.

    Why do you need to know this? If you can give a good answear to that
    you may get better answears.

    It is possible to write a class that uses the class path and search
    each entry (directory or jar-file) for the class you are in, that is
    quite easy, but it may fail on some classes as stated above.

    /robo
    Robert Olofsson, Oct 20, 2003
    #3
  4. James

    John Guest

    I'm not sure about the original poster, but I am new to Java, and I have
    wanted to do this on two separate occasion. In both cases I wanted to
    reference files from the relative directory location of my classes. In the
    first, I wanted to read a configuration file located in WEB-INF. My JAR was
    located in WEB-INF\lib. I had to resort to passing a parameter to the
    servlet, specifying the path to the config file. This complicates deployment
    of my servlet.
    In the other case, I had JUnit tests that loaded test data files. I wanted
    to use a relative directory path to allow anyone to do a get from the root
    of my project in version control, and be able to execute the tests without
    setting up their environment. I resorted to defining a single static field
    in my test class which defines the absolute directory location. This is not
    ideal, since everyone has to change this for their environment.

    "Robert Olofsson" <> wrote in message
    news:bn0eqa$p41$...
    > James () wrote:
    > : anyone can tell me how to get back the path of my current class that

    runnig?
    >
    > Basically you can not do that. The current class may come from a class
    > loader that generates classes by computation only, what would be the
    > path of such classes.
    >
    > Why do you need to know this? If you can give a good answear to that
    > you may get better answears.
    >
    > It is possible to write a class that uses the class path and search
    > each entry (directory or jar-file) for the class you are in, that is
    > quite easy, but it may fail on some classes as stated above.
    >
    > /robo
    John, Oct 20, 2003
    #4
  5. "John" <> wrote in message news:<ZjQkb.836720$uu5.147832@sccrnsc04>...
    > I'm not sure about the original poster, but I am new to Java, and I have
    > wanted to do this on two separate occasion. In both cases I wanted to
    > reference files from the relative directory location of my classes. In the
    > first, I wanted to read a configuration file located in WEB-INF. My JAR was
    > located in WEB-INF\lib. I had to resort to passing a parameter to the
    > servlet, specifying the path to the config file.


    For this case, use getServletContext().getRealPath(relativePath)

    > In the other case, I had JUnit tests that loaded test data files. I wanted
    > to use a relative directory path to allow anyone to do a get from the root
    > of my project in version control, and be able to execute the tests without
    > setting up their environment.


    My most recent kludge for this case is

    URL dirUrl = MyTestClass.class.getResource("./"); // get my
    directory
    URL fileUrl = new URL(dirUrl, "../../data.txt"); // get a related
    file
    String path = fileUrl.getPath().replaceAll("%20", " "); // fix
    escaped spaces

    All of these steps are suspect so you want to test the results, e.g.,
    with File(...).exists(), before running any code and have a backup
    plan, e.g., print a message saying "sorry, you'll need to set the test
    directory manually."

    Not something for external users but perhaps OK as a time saver for
    people configuring and testing your app.
    Chris Riesbeck, Oct 20, 2003
    #5
  6. James

    Phil... Guest

    What happens if say I am sitting in directory /usr/phil when I run
    the program (that happens to live in /usr/bill)
    via the console window "java ../bill/whereami"
    what would be the result of getResource("./");
    would it be /usr/phil or /usr/bill

    "Chris Riesbeck" <> wrote in message
    news:...
    > "John" <> wrote in message

    news:<ZjQkb.836720$uu5.147832@sccrnsc04>...
    > > I'm not sure about the original poster, but I am new to Java, and I have
    > > wanted to do this on two separate occasion. In both cases I wanted to
    > > reference files from the relative directory location of my classes. In

    the
    > > first, I wanted to read a configuration file located in WEB-INF. My JAR

    was
    > > located in WEB-INF\lib. I had to resort to passing a parameter to the
    > > servlet, specifying the path to the config file.

    >
    > For this case, use getServletContext().getRealPath(relativePath)
    >
    > > In the other case, I had JUnit tests that loaded test data files. I

    wanted
    > > to use a relative directory path to allow anyone to do a get from the

    root
    > > of my project in version control, and be able to execute the tests

    without
    > > setting up their environment.

    >
    > My most recent kludge for this case is
    >
    > URL dirUrl = MyTestClass.class.getResource("./"); // get my
    > directory
    > URL fileUrl = new URL(dirUrl, "../../data.txt"); // get a related
    > file
    > String path = fileUrl.getPath().replaceAll("%20", " "); // fix
    > escaped spaces
    >
    > All of these steps are suspect so you want to test the results, e.g.,
    > with File(...).exists(), before running any code and have a backup
    > plan, e.g., print a message saying "sorry, you'll need to set the test
    > directory manually."
    >
    > Not something for external users but perhaps OK as a time saver for
    > people configuring and testing your app.
    Phil..., Oct 22, 2003
    #6
  7. "Phil..." <> wrote in message news:<ksmlb.516488$>...
    > What happens if say I am sitting in directory /usr/phil when I run
    > the program (that happens to live in /usr/bill)
    > via the console window "java ../bill/whereami"
    > what would be the result of getResource("./");
    > would it be /usr/phil or /usr/bill
    >
    > "Chris Riesbeck" <> wrote in message
    > news:...
    > >
    > > My most recent kludge for this case is
    > >
    > > URL dirUrl = MyTestClass.class.getResource("./"); // get my
    > > directory
    > > URL fileUrl = new URL(dirUrl, "../../data.txt"); // get a related
    > > file
    > > String path = fileUrl.getPath().replaceAll("%20", " "); // fix
    > > escaped spaces


    Where you're sitting shouldn't make any difference.
    MyTestClass.class getResource() returns a URL based on where
    MyTestClass is sitting.
    Chris Riesbeck, Oct 22, 2003
    #7
    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. Chung

    How to get current folder path?

    Chung, Nov 17, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    10,262
    Nazer
    Jan 20, 2008
  2. PHaroZ
    Replies:
    5
    Views:
    94,749
    akash02sap
    Aug 17, 2012
  3. Eranga Udesh

    Howto get the current file path with SSI

    Eranga Udesh, Apr 5, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,088
    Aaron Bertrand [MVP]
    Apr 6, 2004
  4. John Smith
    Replies:
    9
    Views:
    461
  5. mathieu

    Path to the current running executable

    mathieu, Jun 25, 2008, in forum: C Programming
    Replies:
    8
    Views:
    302
Loading...

Share This Page