I am trying to execute a batch file which in turn executes an exe and writes output to a file, through java. I do not know why but the output file is not always written. Sometimes the program terminates without writing the file and sometimes it does. Any suggestions? Is there anyway i can wait till the execution of the batch file is complete?Will be great if somebody can help!thanks.
Dani AI
Generated
As requested, posted the code that launches a Windows batch from Java and the external output file is only sometimes produced. The problem is almost always environmental or timing-related; the Java process either does not wait for the external work to finish, the file handles are not flushed/closed before the external program reads them, or the batch/exe spawns a background process and exits early. The practical fixes below are concise and testable.
Checklist to apply (in this order)
- Make sure any Java writers/streams that produced the input file are flushed and closed before launching the batch.
- Launch the batch with a proper process handle (use ProcessBuilder or Runtime.exec with "cmd /c" on Windows) and set the working directory or use absolute paths so redirection writes where you expect.
- Consume or redirect the child process stdout/stderr (or use ProcessBuilder.redirectOutput) so the child cannot block on a full buffer.
- Call waitFor() on the Process and check the exit value to know when the batch has actually finished.
- If the batch uses cmd "start" or otherwise launches the exe asynchronously, modify the batch to run it synchronously or have the batch wait for the exe to finish — waitFor only waits for the process you started (the shell), not arbitrary grandchildren.
Minimal example pattern (adapt paths)
ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "xsbexec.bat");
pb.directory(new File("C:\\full\\path\\to\\bat\\dir"));
pb.redirectErrorStream(true);
pb.redirectOutput(new File("C:\\full\\path\\to\\xsbex2.out"));
Process p = pb.start();
int rc = p.waitFor(); // blocks until the shell exits Diagnostics if the problem persists
- Run the batch manually from the same account/dir and compare timestamps.
- Log the child's exit code and full stdout/stderr.
- Temporarily modify the batch to echo debug messages and pause so you can see whether it returns immediately.
Relevant API reference: ProcessBuilder and Process.waitFor() docs (ProcessBuilder, Process.waitFor).
sidereal 0 Newbie Poster
post the code
uth 0 Newbie Poster
Thanks. The following code is inside a function within a class. Now there is an application called triple used for inferencing. It is an exe but I am not able to invoke it directly. Thats why all the hassle. It consists of two parts.The first part as shown below calls "Commandline" which generates a file. Then when I call the xsbexec.bat, it takes the file thats generated and uses it to generate an output. That i'm redirecting to another file.
String[] args1 = new String[2];
args1[0] = "copyTriple.triple";
// "copyTriple.triple";
args1[1] = "Ignored arguments";
System.out.println("Calling commandline main");
Commandline.main(args1);
try {
Runtime rt = Runtime.getRuntime();
String[] args2 = { "xsbexec.bat" };
rt.exec(args2);
fout1.close(); p1.close();
boolean success = (new File("copyTriple.triple")).delete();
if (!success) {
System.out.println("copyTriple not deleted");
}
System.out.println("done");
}
catch (IOException ioe) {
System.out.println(ioe);
}
The following is what the batch file contains:
@echo off
rem adapt paths here
set XSB=C:\XSB\config\x86-pc-windows\bin\xsb
rem end adapt
echo running %1 %2 %3 %4 %5 %6 %7 %8 %9
%XSB% --nobanner --quietload --noprompt -e "['C:\Documents and Settings\Uthru\workspace\Patient records\#out']." >xsbex2.out
echo done.
exit
Thanks a tonne!
post the code
Edited by Member #120589 because: fixed formatting
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.