Changing Working Directory in Java

D

Dave Monroe

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
 
R

Roedy Green

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
 
G

Gordon Beaton

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
 
D

Dale King

Gordon Beaton said:
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.
 
T

Thomas Weidenfeller

Dale King said:
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
 
G

Gordon Beaton

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
 
D

Dave Monroe

Dale King said:
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
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top