aspose file tools*
The moose likes Java in General and the fly likes Runtime.exec() file problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Runtime.exec() file problem" Watch "Runtime.exec() file problem" New topic
Author

Runtime.exec() file problem

Trish Hartnett
Greenhorn

Joined: Apr 29, 2003
Posts: 18
Hello all,
I've read all the information I can find on Runtime.exec() and I'm stumped. I'm using Java 1.4 on Windows 2000.
The problem:
I want to run a third party .exe called shp2sdo.exe from Runtime.exec(). shp2sdo takes a file and outputs 3 files (.sql, .ctl and .dbf)
The problem is that although when I run my code Runtime does get shp2sdo to output the relevent files, the rest of my code doesn't "see" the outputted files.
I've included portions of my code, if anyone could suggest anything I'd be grateful because I am well and truly stumped here.

Code for running shp2sdo with Runtime:
"
String shp2sdoString = " C:\\shp2sdo.exe C:\\upload\\" + rootFilename + " C:\\" + rootFilename + " \n";
String[] command = {"cmd.exe", "/C", shp2sdoString};
boolean wait =false;
try {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(command);
} catch (IOException e) {
e.printStackTrace();
} catch(InterruptedException e){
e.printStackTrace();
}
"

Then later on another method tries to access the files produced by shp2sdo. And the exception I keep on getting is :
"
14:21:36,468 INFO [STDOUT] Runtime running statement
14:21:36,488 ERROR [STDERR] java.io.FileNotFoundException: C:\eg.sql (The system cannot find the file specified)
"
Which is great, but the file *is* there when I check in Windows Explorer .
Thanks in advance,
Trish.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
It's hard to know when a process started by runtime exec is truly done. Maybe you're looking for the files before they are there.
I've used the waitfor() method on the process returned by exec, but even that returns before all the IO is done on some commands.
I also tried starting threads to pick up stdout and errout from the command I'm running. They send events back to the main thread at end of stream - when the command closes them. I think that works, but haven't beaten it up sufficiently in testing yet.
Just in case it's useful, here's my base stream reader thread. For the above technique, see how it notifies a listener on close.

[ July 16, 2003: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Trish Hartnett
Greenhorn

Joined: Apr 29, 2003
Posts: 18
Thanks for replying,
I tried using the waitFor() method:
"
try {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(command);
proc.waitFor();
System.out.println("Process exit code is: " + proc.exitValue());
} catch (IOException e) {
e.printStackTrace();
} catch(InterruptedException e){
e.printStackTrace();
}
"
and I got this error message:
"
15:06:15,441 ERROR [STDERR] java.lang.InterruptedException
15:06:15,441 ERROR [STDERR] at java.lang.Win32Process.waitFor(Native Method)
"
Which is usually caused, as far as I know, by threads which are waiting for a long time and then are interrupted by another thread.
The thing is that I haven't in any wait() methods in this code.
Any suggestions ?
Thanks,
Trish.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
I think it is trying to write something to stderr but you are not reading that stream. You should get the streams for stderr and stdout and read them so that the process can complete. Creating a separate Thread to do this works.
See the java.lang.Process JavaDocs.
Bill
[ July 16, 2003: Message edited by: William Brogden ]
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
William is right on this. If you start a child process with Runtime.exec() you absolutely must consume all output, stdout and stderr, from the child. The bug parade is replete with problems like this and it is Sun's contention that it is not a bug but a misunderstanding on the part of the programmer of what is required.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Trish Hartnett
Greenhorn

Joined: Apr 29, 2003
Posts: 18
yup, mea culpa.
I read all the documentation and still didn't cop on.
Thanks for all the advice,
Trish.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Runtime.exec() file problem