Changing Working Directory in Java

Discussion in 'Java' started by Dave Monroe, Oct 6, 2003.

  1. Dave Monroe

    Dave Monroe Guest

    I want to change my current working directory to a directory
    containing log files and exec a command to comb through the logs and
    return strings from the logs.

    There doesn't appear to be a good way to do this from within the Java
    program.

    What am I missing?

    TIA

    Dave Monroe
     
    Dave Monroe, Oct 6, 2003
    #1
    1. Advertising

  2. Dave Monroe

    Roedy Green Guest

    On 6 Oct 2003 12:25:44 -0700, (Dave Monroe)
    wrote or quoted :

    >I want to change my current working directory to a directory
    >containing log files and exec a command to comb through the logs and
    >return strings from the logs.
    >
    >There doesn't appear to be a good way to do this from within the Java
    >program.
    >
    >What am I missing?

    The problem is each thread would need its own current working
    directory, which could get confusing.

    So you do it manually with new File (mycurrentdir, filename)

    For other such trivia 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, Oct 6, 2003
    #2
    1. Advertising

  3. On 6 Oct 2003 12:25:44 -0700, Dave Monroe wrote:
    > I want to change my current working directory to a directory
    > containing log files and exec a command to comb through the logs and
    > return strings from the logs.
    >
    > There doesn't appear to be a good way to do this from within the
    > Java program.
    >
    > What am I missing?


    First off, you can't change the cwd of the JVM.

    If you really need to be in a certain directory when you run your
    application, change to that directory before starting it, perhaps in a
    script for starting the application.

    Or, you can simply specify the directory as part of the filenames you
    need to open. In particular the java.io.File constructor that lets you
    specify the directory and filename separately might be most
    appropriate.

    You say that you will exec a command, which I understand to mean that
    you intend to use Runtime.exec() to invoke an external program. You
    can use the version of Runtime.exec() that lets you specify a working
    directory for the external program, so the JVM itself doesn't need to
    change directories.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Oct 6, 2003
    #3
  4. Dave Monroe

    Dale King Guest

    "Gordon Beaton" <> wrote in message
    news:3f81c6af$...
    > On 6 Oct 2003 12:25:44 -0700, Dave Monroe wrote:
    > > I want to change my current working directory to a directory
    > > containing log files and exec a command to comb through the logs and
    > > return strings from the logs.

    >
    > You say that you will exec a command, which I understand to mean that
    > you intend to use Runtime.exec() to invoke an external program. You
    > can use the version of Runtime.exec() that lets you specify a working
    > directory for the external program, so the JVM itself doesn't need to
    > change directories.



    FYI, that version of Runtime.exec() was not introduced until 1.3 so if you
    are using an older VM you are out of luck.
    --
    Dale King
     
    Dale King, Oct 7, 2003
    #4
  5. "Dale King" <> writes:
    > FYI, that version of Runtime.exec() was not introduced until 1.3 so if you
    > are using an older VM you are out of luck.


    You can still wrap your external program in some script that first
    changes the cwd and than starts the program. You might however get in
    trouble with the limitations of the child process management of
    Runtime.exec().


    /Thomas
     
    Thomas Weidenfeller, Oct 7, 2003
    #5
  6. On Tue, 7 Oct 2003 09:12:30 +0000 (UTC), Thomas Weidenfeller wrote:
    > You can still wrap your external program in some script that first
    > changes the cwd and than starts the program. You might however get
    > in trouble with the limitations of the child process management of
    > Runtime.exec().


    That's remedied easily enough (assuming unix or similar) by using exec
    i.e. replace the script process with the application itself after
    changing the directory, allowing "myApp" to be the immediate child of
    the JVM:

    #!/bin/sh

    cd whatever
    exec myApp "$@"

    If you parameterise the directory, the application and its arguments,
    only one helper script is necessary for all of your Runtime.exec()
    needs:

    #!/bin/sh

    cd $1
    shift
    exec "$@"

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Oct 7, 2003
    #6
  7. Dave Monroe

    Dave Monroe Guest

    "Dale King" <> wrote in message news:<>...
    > "Gordon Beaton" <> wrote in message
    > news:3f81c6af$...
    > > On 6 Oct 2003 12:25:44 -0700, Dave Monroe wrote:
    > > > I want to change my current working directory to a directory
    > > > containing log files and exec a command to comb through the logs and
    > > > return strings from the logs.

    > >
    > > You say that you will exec a command, which I understand to mean that
    > > you intend to use Runtime.exec() to invoke an external program. You
    > > can use the version of Runtime.exec() that lets you specify a working
    > > directory for the external program, so the JVM itself doesn't need to
    > > change directories.

    >
    >
    > FYI, that version of Runtime.exec() was not introduced until 1.3 so if you
    > are using an older VM you are out of luck.


    You are gentlemen and scholars one and all.

    The more I've looked into this, it's consistent with Java's platform
    independence philosophy that there's no good way to traverse a given
    file system since they vary rather dramatically.

    FYI - I'm using Java 1.4; latest stable version.

    Thanks for your help.

    Dave Monroe
     
    Dave Monroe, Oct 7, 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. Samridhi Kumar Shukla
    Replies:
    1
    Views:
    525
    Alvin Bruney
    Nov 30, 2003
  2. Ahmed Moustafa
    Replies:
    1
    Views:
    1,709
  3. Priyanka AGARWAL
    Replies:
    9
    Views:
    9,978
    Gordon Beaton
    May 25, 2004
  4. hack_tick

    changing working directory?

    hack_tick, Oct 24, 2005, in forum: Java
    Replies:
    4
    Views:
    437
    hack_tick
    Oct 24, 2005
  5. Aaron Beall
    Replies:
    2
    Views:
    1,811
    Aaron Beall
    Sep 14, 2007
Loading...

Share This Page