1

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.");
    }
Community
  • 1
  • 1
Eric D
  • 1,363
  • 13
  • 9
  • 1. JNA is not safe for old programmers too, at the beginning twice. 2. At first sight exception has nothing to Java, but to underlying layer (You say manuall copying has problems too) – Jacek Cz Jan 13 '17 at 09:30
  • Java's FileUtils ultimately will also use the Filesystem / OS at some point. Only it is done by the VM native implementation. Using JNA will make things only less convenient. What stops you from simply handling the IOExceptions? Or even better: Work on fixing the congestions! (Well, best to do both, I guess.) – Fildor Jan 13 '17 at 09:51
  • "An unexpected network error occurred" is an Error Message from Windows: https://support.microsoft.com/en-us/kb/2649905 – tak3shi Jan 13 '17 at 12:23
  • @JacekCz Manual copying does succeed. I just figure that it's more robust to use native Windows routines which implement their own failure handling code. @ Fildor My organization (a school) has won a multi-million dollar lawsuit against our network provider but that does nothing to fix network congestion problems :(. Since I don't know enough about the exception I am loathe to build a `do... while` error handling routine for `An unexpected network error occurred`. I'd rather have Windows handle it since Window's own File Explorer does handle the copying cleanly. – Eric D Jan 13 '17 at 15:53
  • Eric, I agree with @Fildor, java have NOT magic way to copy files via network, out of operating system, files etc. – Jacek Cz Jan 13 '17 at 16:07
  • next ... from exception get clause, and additional info, it is for You. (Fildro said the same idea) Do You buy new car when bulbs are finished? Get SOURCE of problem. Manually works .... maybe is slower, less demanding in threads, network lock etc??? Who knows ... – Jacek Cz Jan 13 '17 at 16:10
  • I feel like I should delete this question (if I can). It feels too general to attract an appropriate answer. Is it just clogging up stackoverflow? Never mind. I may try using the "DOS" copy and move commands with appropriate wild card, called through ProcessBuilder, in which case I'd have an answer to my own question. – Eric D Jan 14 '17 at 12:36

0 Answers0