The Java program I created in Windows takes a PDF, processes it (using sejda FYI) and then copies the results to a network share.
The network we're on is frequently congested (I cannot fix that) so in Java the program sometimes throws an IOException
when copying the files: An unexpected network error occurred
.
Manually copying (the small) files using Windows Explorer can take forever but will eventually succeed. Windows Explorer can cope with the network congestion.
The Java methods I've called don't have the same ability to complete a copy on a congested network without throwing an IOException
.
I've contemplated putting the copyFile
call into a loop and running it (with a Thread.sleep()
delay) until the IOException
isn't thrown and until all files are copied but that doesn't seem like a good solution. It feels very brute force.
Instead, what I'm thinking is that Windows probably does the best job of handling network copies so I'd like to use JNA or Java Native Access to call upon Windows to copy the files across the network rather than rely on internal Java methods (the Java methods seem not able to handle network copies seamlessly).
I've found two excellent Stackoverflow Q&As which address copying in Java (Copying files from one directory to another in Java, Standard concise way to copy a file in Java?) but they don't address using native file copy routines. I've also found a hint that the JNA or Java Native Access would provide exactly what I'm looking for but am not sure where to start.
I suppose I could call Windows own copy and move commands from the command line using ProcessBuilder but that also does not feel like a satisfactory solution. My gut tells me to the JNA route (either that or rewrite in VB.Net since it's not a particularly complicated program).
Amusingly the project refers to StackOverflow for help but I can't find anything on StackOverflow regarding file copying :).
FYI This is a code snippet of what fails.
try {
FileUtils.copyFile(FileUtils.getFile(oldFile), FileUtils.getFile(newFile));
} catch (IOException e) {
e.printStackTrace();
throw new IOException(e.getMessage() + " Try again. The network may be working against you.");
}