7

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.

Community
  • 1
  • 1
Andy
  • 3,600
  • 12
  • 53
  • 84
  • Perhaps check out http://stackoverflow.com/questions/6674431/possible-causes-of-java-io-ioexception-createprocess-error-5 – demongolem Sep 06 '13 at 16:03
  • @demongolem I know the info on that question. It doesn't help me solve my problem though – Andy Sep 06 '13 at 19:16

5 Answers5

4

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();
Lunchbox
  • 1,603
  • 11
  • 14
  • +1 Thank you for your answer. This is not the issue in my case (as I have managed to solve it), but I may use this location as a fail safe to the temporary file. Would this indeed be the next best place to put it? – Andy Sep 12 '13 at 18:24
  • Sorry, read your solution after posting my answer. The user should always have access to this [folder](http://en.wikipedia.org/wiki/Home_directory). It's only okay if you are planning on cleaning up after yourself -- which your code is. I'd probably put it into a 'hidden' folder though (answer updated). – Lunchbox Sep 13 '13 at 21:53
  • Thanks for the update, that makes sense. I'll make the home directory the fail safe temporary directory then. – Andy Sep 14 '13 at 15:27
2
  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)

Community
  • 1
  • 1
Spektre
  • 49,595
  • 11
  • 110
  • 380
  • Thanks for your answer. I thought you had hit it with point one but it seems not. It seems that I cannot execute anything in the Temp directory, including executables that are already there, even when I right click and press "Run as administrator", so it is more of an OS related issue. However +1 for the in-depth answer! :) – Andy Sep 06 '13 at 18:40
2

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

Ryan Hurling
  • 288
  • 3
  • 9
  • Thanks for your answer. I do know of `Desktop` but I never actually considered using it for this. Unfortunately, it wouldn't have solved anything though because the issue was related to Windows, not Java. – Andy Sep 12 '13 at 18:27
  • ahh okay, i thought it might help because i tried it the way you had before which also gave me the error 'access denied', but the Desktop object helped me around it. if you haven't tried it, id recommend it.. nothing to lose but you never know :) – Ryan Hurling Sep 12 '13 at 18:31
  • I would try it, but it is kind of a pain to reverse the ownership changes I made, and I'm quite sure that I would still have got the same error as the issue is not related. Plus, I'm not sure if I can pass a command line parameter using the Desktop object. I will be sure to use it in the future though – Andy Sep 12 '13 at 18:56
  • i don't think you can pass parameters with the Dekstop object no, my mistake, i wasn't aware that you needed to :) – Ryan Hurling Sep 12 '13 at 18:58
  • It's ok, it doesn't matter too much. I didn't explicitly state that I needed to, but it could be inferred by my code :) – Andy Sep 12 '13 at 19:04
1

Seeing as though it has only been touched on here, I will say that the issue was related to permissions in Windows, and is not anything to do with Java.

As stated in the SuperUser question I've linked to in my original question, I found that my usual account did not have ownership of that folder for some unknown reason - so nothing could be executed; it wasn't just the temporary file I had created in Java.

Even though I am an administrator, in order to take ownership of the folder I had to enable the Built-In administrator account and grant myself ownership. Since I did that, it has all worked as expected.

Thanks to all for their efforts, I will award the bounty to the answer that was most detailed and put me on the right tracks.

Andy
  • 3,600
  • 12
  • 53
  • 84
0

What version of Windows are you running? Microsoft significantly tightened the restrictions around executing programs in Windows 7. My guess is that it the OS won't allow you to fork something that wasn't authenticated at the time your program was launched. I'd try running it on Windows 2000 or XP and see if you have the same issues.

  • Thank you for your answer and sorry for the slow reply. I am running Windows 7, however inspired by your answer I've tried running the executable from the Temp folder on two other Windows 7 machines (which I thought I had set up in the same way), and the executable ran fine on those machines. Any idea what could be denying me access on my laptop? – Andy Sep 08 '13 at 09:48