2

I have the following code:

try {
    Files.copy(srcFile.toPath(), destFile.toPath(), copyOptions);
} catch (IOException exc) {
    System.out.println("IOException on copy: " + exc.getMessage());
    System.out.println("Files.copy(" + srcFile.toPath() + ", " + destFile.toPath() + ", copyOptions)");
    System.out.println(srcFile.toPath() + " readable is " + srcFile.canRead());
}

This fails for certain PDF files. There is something about these particular PDFs that requires admin rights to copy them. (They are readable, the destination folder is writable; I can replicate the UAC challenge if I try to copy in a Command Prompt.)

I would like to elevate permissions as atomically as possible -- not for my entire program. Ideally, I would hit the first such access denied problem, prompt the user if they want me to copy it anyway, in a yes/no/always/never sort of choice dialog.

If someone wishes, I can write a tiny program which does the above Files.copy, and I might be able to supply one of these persnickety PDFs -- though the one I am working with is a customer's so I would have to look elsewhere or go to great trouble (e.g., with PDFbox) to obscure the text.

I realize there is probably info out there about why some PDFs do this, and I will admit I am ignorant in this area, but that is really rather irrelevant to me, unless -- and this would be a big advantage, but I would still need the privilege solution -- I could detect that a particular file has this "you can't copy me" aspect.

Thanks for any help!

Ok, apparently there is nothing magical about these PDF files, they were just created with some restrictive permissions:

enter image description here

Tamias
  • 173
  • 9
  • You cannot have your program itself elevate it's privileges. I have the same issue here in the office on several of my programs and the only solution I had was to have the programs run elevated from the get go. Because of this, all Java programs I develop here that are ran company wide are web start to ensure that signed code is the only thing ran. – Rabbit Guy May 11 '16 at 19:28
  • The only trick that comes into mind is to run another JVM with admin rights using exec utility. See: http://stackoverflow.com/a/14596649/1944056 – mpasko256 May 11 '16 at 19:36
  • @blahfunk, thanks for the reply. So, if my program has to run as elevated from the get-go, is there a way the program could "act as if" it were an ordinary user most of the time? I expect not, but I had to ask. – Tamias May 11 '16 at 19:38
  • @mpasko, thanks, had looked at some similar past postings but missed that one. I am thinking I'll batch up all such failed copy requests (I already do something similar with oversize files) and prompt the user once for all of them, at which point I could (If I understand correctly) kick off something like that. – Tamias May 11 '16 at 19:44
  • I mean, if you want you can always build the checks into your program itself so that it isn't doing elevated actions without your program giving the okay, but otherwise, no. Your applications run and stay at the user level you started them on. – Rabbit Guy May 11 '16 at 19:52
  • Thanks for the helpful comments, I bookmarked the run-command-prompt-as-administrator page. I may just prompt user to re-run as admin. I did realize that after a failure, in order to get more info, file.canRead() returns true whereas Files.isReadable() more helpfully returns false. I probably should have been using that to begin with, rather than the first thing I saw in Eclipse that sounded right. A bit more on that difference is at https://docs.oracle.com/javase/tutorial/essential/io/legacy.html. – Tamias May 12 '16 at 12:04

0 Answers0