Access denied when trying to execute a .exe in %AppData%

Andy picture Andy · Sep 1, 2013 · Viewed 8k times · Source

I'm trying to use RemoveDrive.exe, found here, in my Java application. I have it in my JAR, and I'm extracting it to a temporary file using the following code, however when I try to run it I get an IOException which says CreateProcess error=5, Access is denied. The program doesn't normally need admin priviledges though. Any ideas on what could be causing the issue?

            File RDexe = File.createTempFile("rmvd", ".exe");

            InputStream exesrc = (InputStream) GraphicUI.class.getResource("RemoveDrive.exe").openStream();
            FileOutputStream out = new FileOutputStream(RDexe);

            byte[] temp = new byte[1024];
            int rc;

            while((rc = exesrc.read(temp)) > 0)
                out.write(temp, 0, rc);

            exesrc.close();
            out.close();

            RDexe.deleteOnExit();

            // run executable
            Runtime runtime = Runtime.getRuntime();
            System.out.println(RDexe.getPath() + " " + "F:\\" + " -b -s");
            Process proc = runtime.exec(RDexe.getPath() + " " + "F:\\" + " -b");
            InputStream is = proc.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));

            String line; boolean ejected = false;
            while((line = reader.readLine()) != null)
                if(line.equalsIgnoreCase("failed")) ejected = false;
                else if(line.equalsIgnoreCase("success")) ejected = true;

            reader.close();
            is.close();

UPDATE: If I enable the built-in Administrator account (net user administrator /active:yes), everything works fine from there. However if I right click and run as administrator in my standard account, I still get the error and UAC doesn't even ask for permission.

EDIT: Seeing as though the bounty is nearly finished, please see my SuperUser question which has helped me solve this problem... I'll be awarding the bounty and accepting an answer soon.

Answer

Lunchbox picture Lunchbox · Sep 11, 2013

This may not be the problem in your situation, but some anti-virus programs will prevent executables or scripts inside temporary folders from being run. Instead of creating a temporary file, try putting it in the user directory:

File rdExe = new File(System.getProperty("user.home") + "/.yourProgramName/rmvd.exe");
rdExe.getParentFile().mkdirs();