Changing the program's current directory.

D

Dave Rudolf

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
 
G

Gordon Beaton

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
 
D

Daniel Pitts

Dave said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top