Changing the program's current directory.

Discussion in 'Java' started by Dave Rudolf, Jan 16, 2007.

  1. Dave Rudolf

    Dave Rudolf Guest

    Hey all,

    I am writing a java program that is really a front-end to a handful of other
    programs, each in a different location of a directory tree. Some of the
    programs have data files that they look for w.r.t. their current directory.
    Now, when I call
    Runtime.exec( "some_dir\\some_program.exe" );
    it starts the program, but has it running from whatever the java program's
    current directory is. Thus, the programs can't find the data files that they need.

    So, I can get the current directory by doing
    System.getProperty( "user.dir" );
    But, apparently I can't just do
    System.setProperty( "user.dir", "some_dir" );
    It seems that doing so changes the system property at least as far as the
    property map is concerned, but it does not affect the actual working directory.

    So, how might I actually get the thing to change directories?

    Thanks.

    Dave
    Dave Rudolf, Jan 16, 2007
    #1
    1. Advertising

  2. On Tue, 16 Jan 2007 12:18:28 -0600, Dave Rudolf wrote:
    > Now, when I call
    > Runtime.exec( "some_dir\\some_program.exe" );
    > it starts the program, but has it running from whatever the java
    > program's current directory is. Thus, the programs can't find the
    > data files that they need.


    [...]

    > So, how might I actually get the thing to change directories?


    You can't change the working directory of the JVM, but you don't need
    to in order to achieve what you've described.

    Runtime.exec() lets you specify a working directory for the target
    process.

    /gordon

    --
    [ don't email me support questions or followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Jan 16, 2007
    #2
    1. Advertising

  3. Dave Rudolf

    Daniel Pitts Guest

    Dave Rudolf wrote:
    > Hey all,
    >
    > I am writing a java program that is really a front-end to a handful of other
    > programs, each in a different location of a directory tree. Some of the
    > programs have data files that they look for w.r.t. their current directory.
    > Now, when I call
    > Runtime.exec( "some_dir\\some_program.exe" );
    > it starts the program, but has it running from whatever the java program's
    > current directory is. Thus, the programs can't find the data files that they need.
    >
    > So, I can get the current directory by doing
    > System.getProperty( "user.dir" );
    > But, apparently I can't just do
    > System.setProperty( "user.dir", "some_dir" );
    > It seems that doing so changes the system property at least as far as the
    > property map is concerned, but it does not affect the actual working directory.
    >
    > So, how might I actually get the thing to change directories?
    >
    > Thanks.
    >
    > Dave


    Instead of using Runtime.exec, look into ProcessBuilder.
    Both approaches let you run the program in a specific working
    directory, but ProcessBuilder is more flexible in how you do it.
    Daniel Pitts, Jan 17, 2007
    #3
    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. Abby
    Replies:
    1
    Views:
    494
    Jack Klein
    Aug 29, 2003
  2. Andy B.
    Replies:
    4
    Views:
    24,352
    firebush
    Jan 30, 2011
  3. Peter Hansen
    Replies:
    1
    Views:
    328
    Mike Meyer
    Dec 19, 2005
  4. vsoler

    Changing the current directory

    vsoler, Nov 15, 2009, in forum: Python
    Replies:
    0
    Views:
    359
    vsoler
    Nov 15, 2009
  5. vsoler
    Replies:
    8
    Views:
    666
    Gabriel Genellina
    Nov 16, 2009
Loading...

Share This Page