Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Martin Gregorie Newsgroups: comp.lang.java.programmer Subject: Re: A "current directory" concept for Java... Date: Sat, 23 Aug 2014 19:34:12 +0000 (UTC) Organization: A noiseless patient Spider Lines: 64 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Sat, 23 Aug 2014 19:34:12 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="e5160e2be48f1ea550cf61512446f525"; logging-data="17132"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+perjteQiuajVyhbGSv+PW+s85hbgBPwU=" User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508 git://git.gnome.org/pan2) Cancel-Lock: sha1:JpgrsViSnTcFBsO3LZrubuR7dns= Xref: csiph.com comp.lang.java.programmer:29827 On Sat, 23 Aug 2014 16:31:36 +0000, Andreas Leitgeb wrote: > Martin Gregorie wrote: >> On Fri, 22 Aug 2014 23:38:43 +0000, Andreas Leitgeb wrote: >>> I'll need to emulate some kind of current directory, >> If you haven't already done so, take a look at File.listFiles() and its >> filter-equipped siblings. > > Yep, I think I also missed that in Path... Good to know that I'll just > have to use (good old) java.io for that. > >>> Eventually I need to spawn processes, which then need that emulated >>> current directory to be their initial real current directory. >> Do these have to be Java processes? > > No. > At least, I already found myself, how to pass a directory to the > ProcessBuilder. :) > Cool. It all looks straightforward - at least for UNIX/Linux. To run 'myprocess' against each file in the directory you'd modify my processDirectory method to look like this: void processDirectory(File dir) { File flist[] = f.listFiles(); for (int i = 0; i < flist.length; i++ { if (flist[i].isDirectory()) processDirectory(new File(dir, flist[i].getName())); else { ProcessBuilder pb = new ProcessBuilder("myprocess", flist[i].getname()); pb.directory(dir); Process p = pb.start(); /* deal with process i/o streams */ int exitValue = p.waitFor(); /* see if the process exited normally */ } } } It looks to me as if ProcessBuilder is very similar to C's exec() functions. The above is not intentionally teaching you to suck eggs: apologies if you already know what it shows. Apart from the point that there doesn't seem to be any way to make connections to the new Process's i/o streams before the command it controls starts to execute I can see only one bijou snaggette which might affect code portability: the meaning of 'current directory'. In a UNIX/Linux environment relative file names are relative to the directory the user is in when a command is issued, but in at least some circumstances Windows programs act as though the 'current directory' is the one the program was loaded from. I've noticed this difference when running Windows programs on a Linux box under Wine but am unclear when it occurs: might it apply when a program is launched by clicking an icon but not when its launched from a command line or a user-written CMD file? -- martin@ | Martin Gregorie gregorie. | Essex, UK org |