Error: java.io.IOException: The handle is invalid

2k views Asked by At

I am trying to move file from one location to another When i run the .jar through command prompt it's working fine but when jar has been trigger from Robot scheduler(with different service account) getting below error

java.io.IOException: The handle is invalid
    at java.io.WinNTFileSystem.canonicalize0(Native Method)
    at java.io.WinNTFileSystem.canonicalize(Unknown Source)
    at java.io.File.getCanonicalPath(Unknown Source)
    at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:1076)
    at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:1040)

The method which i am using for fileMove is below

public static Boolean fileMove(File source, String destination) {
    LOG.info(destination + File.separator + source.getName());
    LOG.info(source.getAbsolutePath());
    File dest = new File(destination + File.separator + source.getName());


     try {
         FileUtils.copyFile(source, dest);
         return FileUtils.contentEquals(source, dest) && Files.deleteIfExists(source.toPath());
     } catch (IOException e) {
         LOG.warn("IO Error Occurred during file Operations", e);
         return false;
     }
}

LOG.info(destination + File.separator + source.getName()) is logging below path //uk001/eucl/Email_Broker_Archive\mime-message--7431406574003289236.eml
(destination=//uk001/eucl/Email_Broker_Archive, File.separator='\', source.getName()=mime-message--7431406574003289236.eml

LOG.info(source.getAbsolutePath()) is logging below path

\uk001\eucl\Source\0002\mime-message--7431406574003289236.eml

R/W Permission is there for both service account(cmd and Robot scheduler) and the application is running on windows server.

1

There are 1 answers

1
Karthik R On

It seems to be an issue of File separator not matching the OS's file separator.

//uk001/eucl/Email_Broker_Archive\mime-message--7431406574003289236.eml

Please replace and check if the \ is the issue. Also, do an if check if the file exists before copying the file all the time.

if (!Files.exists(dest)) {
    FileUtils.copyFile(source, dest);
    return FileUtils.contentEquals(source, dest) && Files.deleteIfExists(source.toPath());
//Modify the check above accordingly as the file exists check is already done.
 }