Reputation: 705
I have a problem. I try to copy a file and I get a FileNotFound exception. Here is my code:
File file = new File("C:\\.DS\\tmp\\client-" + node_id + ".war");
File dir = new File("D:\\Utils\\Apache\\Tomcat\\webapps");
try {
FileUtils.copyFileToDirectory(file, dir);
} catch (Exception e) {
e.printStackTrace();
}
And the exception is:
java.io.FileNotFoundException: Source 'C:\.DS\tmp\client-022.war' does not exist
at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:1074)
at org.apache.commons.io.FileUtils.copyFileToDirectory(FileUtils.java:1013)
...
But the file is in that folder.
This code is called from JSF in Tomcat, so maybe it's a problem of Tomcat direcories. The file is generated in previous function via external command using ProcessBuilder, so maybe Java tries to parallel and the ProcessBuilder is finishing after the copying is done.
Also, in another method of the same class this code works perfectly:
File file = new File("C:\\.DS\\tmp\\client-" + node_id + ".properties");
File dir = new File("C:\\.DS\\ss\\engines");
try {
FileUtils.copyFileToDirectory(file, dir);
...
Upvotes: 1
Views: 1458
Reputation: 705
I've figured out that Java is "smart", so Process Builder runs in separate thread (or even process), and to fix my problem I have to change
ProcessBuilder pb = ...
pb.start()
to
ProcessBuilder pb = ...
Process p = pb.start()
p.waitFor()
Upvotes: 1