Runtime.exec on Windows XP

D

David Kensche

Hello,
I want to execute an application from within java. Namely an
implementation of GNU patch. Whereas the following code works
fine on Win2k the Process won't stop on WinXP. On both systems
I have installed the patch.exe in the same directory and added
this directory to the PATH variable.

Does anyone know about special pitfalls on XP regarding
Runtime.exec?

Thanks beforehand,
David

/**
* Patch is given as input via stdin whereas the original is given as
* a temporary file is used as target file.
*/
public synchronized String applyPatch(String orig, PatchScript patch)
throws PatchFailedException {
String revString = null;
try {
logger.debug("Write original to a file.");
File origFile = new File(ORIG_FILE_NAME);
if(!origFile.exists()) origFile.createNewFile();
FileWriter writer = new FileWriter(origFile);
writer.write(orig);
writer.close();
logger.debug(origFile.getAbsolutePath() + " written. File closed.");

logger.debug("Start 'patch' process reading original from temp
file '" + ORIG_FILE_NAME +
"' and patch script from stdin '-'.");
logger.debug("patch =\n" + patch);
Process patchProc = Runtime.getRuntime().exec("patch " +
origFile.getAbsolutePath());

logger.debug("Read patch output from streams.");
new MonitorInputStreamThread(patchProc.getErrorStream()).start();
new MonitorInputStreamThread(patchProc.getInputStream()).start();
logger.debug("Write patch to stdin.");
new WriterThread(patchProc, patch.toString()).start();


logger.debug("Wait for 'patch' to finish.");
logger.debug("patch-status="+patchProc.waitFor());

logger.debug("Reading result.");
BufferedReader reader = new BufferedReader(new FileReader(origFile));
String line = reader.readLine();
if(line != null) revString = line + "\n";
while ((line = reader.readLine()) != null) revString += line + "\n";
reader.close();
logger.debug("patched:\n"+revString);

origFile.delete();

} catch(Exception e) {
throw new PatchFailedException("Could not execute patch script!", e);
}
return revString;
}
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top