Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Changing Working Directory in Java

Reply
Thread Tools

Changing Working Directory in Java

 
 
Dave Monroe
Guest
Posts: n/a
 
      10-06-2003
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
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      10-06-2003
On 6 Oct 2003 12:25:44 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (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.
 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      10-06-2003
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
 
Reply With Quote
 
Dale King
Guest
Posts: n/a
 
      10-06-2003
"Gordon Beaton" <(E-Mail Removed)> wrote in message
news:3f81c6af$(E-Mail Removed)...
> 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


 
Reply With Quote
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      10-07-2003
"Dale King" <(E-Mail Removed)> 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
 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      10-07-2003
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
 
Reply With Quote
 
Dave Monroe
Guest
Posts: n/a
 
      10-07-2003
"Dale King" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> "Gordon Beaton" <(E-Mail Removed)> wrote in message
> news:3f81c6af$(E-Mail Removed)...
> > 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
changing working directory? hack_tick Java 4 10-24-2005 08:56 AM
Runtime.exec() with env and working directory parameters is not working. Priyanka AGARWAL Java 9 05-25-2004 02:34 PM
Difficulty in changing the Connection Server control path changing ... Samridhi Kumar Shukla ASP .Net 1 11-30-2003 02:31 AM
How to get the current working directory in a Java Web Start application Ahmed Moustafa Java 1 09-20-2003 02:33 PM



Advertisments