Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 372
Andy Avatar asked Sep 01 '13 15:09

Andy


People also ask

How do I allow access to an EXE file?

-Right click on the Start button to open the menu, select Run to open the Run box and type netplwiz in the Run box. Press Enter to open the User Accounts settings box. -Click the Properties button and set the level of access you want to grant the user. Select Administrator and click Apply / Accept and exit.

How do I fix access denied error?

Right-click the file or folder, and then click Properties. Click the Security tab. Under Group or user names, click your name to see the permissions that you have. Click Edit, click your name, select the check boxes for the permissions that you must have, and then click OK.

How do I fix access denied folders in Windows 10?

Folder Access Denied You Need Permission Right-click on the problematic folder and press “Properties.” In the “Security” tab, click “Advanced.” Locate “Owner” in the new window that opens and tap “Change.” Type “Users” in the dialogue box provided, then hit “OK.”


3 Answers

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();
like image 66
Lunchbox Avatar answered Oct 20 '22 11:10

Lunchbox


just a heads up on another way to run files, have you thought of using the java Desktop object? : http://docs.oracle.com/javase/6/docs/api/java/awt/Desktop.html

i've found it useful when needing to run programs through my java program. something like this could work for you:

Desktop.getDesktop().open(new File("enter path and name of the file"));

hope you find it useful

like image 42
Ryan Hurling Avatar answered Oct 20 '22 10:10

Ryan Hurling


  1. I am not JAVA user but isn't it 32 vs. 64 bit issue ?

    On 64 bit Windows error code 5 usually means that executable is not 64 bit compatible. Sometimes this is the case even when executable need to access only some (older win) system directory which does not exist anymore. To prove this try to use your executable in command line. if you can manage to get it work there than it is different issue. If not find executable for your OS.

  2. Another possibility is that the file has to be physically present on some drive.

    You wrote that you has it as temporary. Not shore what it means for JAVA. If it only copy it to some file and delete after use than its OK but if it is only in memory somewhere than that could be problem if executable need access to itself. To prove this just copy the file to some known location and then run it from there (in JAVA). if it works than you will need to do something about it (copy and delete executable from JAVA before and after execution to physical disk medium or whatever)

  3. Another possibility is that error code 5 comes from JAVA environment an not from OS

    In that case I have not a clue what it means (not JAVA user)

like image 2
Spektre Avatar answered Oct 20 '22 12:10

Spektre