1

I'm using Apache commons.io library to copy a folder in Windows Programs folder, but when I do that Eclipse Console says:" java.io.IOException: Destination directory cannot be created" How can I resolve it?

My Code:

try {
    File srcDir = new File ("test");
    File destDir = new File ("C:\\Programs\\test");
    FileUtils file = new FileUtils();
    file.copyDirectory(srcDir, destDir);
    System.out.println("Copied!");
    }catch (IOException e) {
        System.out.println("error : "+e);
    }
Gabriel
  • 464
  • 4
  • 17
  • 1
    Have you tried manually creating the directory? Perhaps that will tell you why you can't. This is called **research** and you should always do research before asking others for help. – Andreas Mar 28 '18 at 16:04
  • 2
    You need to run the process as administrator, and maybe take care of the UAC https://stackoverflow.com/questions/4662574/how-do-i-elevate-my-uac-permissions-from-java – alex440 Mar 28 '18 at 16:04
  • @Andreas the program should create that – Gabriel Mar 28 '18 at 16:05
  • 1
    @Gabriel14 That means "no", you didn't **try** that. – Andreas Mar 28 '18 at 16:06
  • Sorry @Andreas, I tried that and it works, but I need the program do that – Gabriel Mar 28 '18 at 16:08
  • It didn't pop up a dialog saying you needed elevated access to create the directory? Or to delete it again? – Andreas Mar 28 '18 at 16:17
  • No, it only prints a IOException in Eclipse Console and the program continue to go EDIT:I changed the directory to Program Files\test, but it still does not work – Gabriel Mar 28 '18 at 16:18
  • @Andreas I found the solution : if I open it with cmd as Administrator it don't gives error, now my question is : how I can start a bat file with administrator privileges from Java code? – Gabriel Mar 28 '18 at 16:30
  • @Gabriel14 When people are nice and leave you comments, the least you can do is read them. The [second comment](https://stackoverflow.com/questions/49539214/destination-directory-cannot-be-created?noredirect=1#comment86085666_49539214) *(the one by alex440)* provided a [link](https://stackoverflow.com/questions/4662574/how-do-i-elevate-my-uac-permissions-from-java) that answers the question about administrator privileges! – Andreas Mar 28 '18 at 17:08

0 Answers0