0

I am running windows 7 and have an exe file I am trying to run using the following command in Java:

File dir = new File("C:\\PATH\\TO\\DIR");
String[] cmdArray = {"file.exe"};

if(dir.exists()){
    for(String s : dir.list()){
        if(s.equals(cmdArray[0]))
            System.out.println("File exists!");
    }
}

Runtime.getRuntime().exec(cmdArray,null,dir);

The exec(...) command, however, gives me this error:

Cannot run program "file.exe" (in directory "C:\PATH\TO\DIR"): CreateProcess error=2, The system cannot find the file specified

The above code does print out "File Exists!" so the file is there and Java knows it is there. I dont know how this is possible.

Also, I need to use Runtime.getRuntime().exex(...). This call is actually inside another method that I am not suppose to change so alternative solutions will not work for me. Thanks in advance!

EDIT: I ran the file in a cmd window without admin privileges and got the following message:

Error in Opening Configuration File in Read Mode

So I assume I have permission issues. How would I change the permissions in order for me to be able to run it?

EDIT2:

I tried changing cmdArray to this:

String[] cmdArray = {"C:\\PATH\\TO\\DIR\\file.exe"};

and it worked like a charm. One question though. Why did this work but not the other way? I assumed that Runtime.exec() ran at a high level like so in a cmd wibndow:

pushd dir
cmdArray[0] cmdArray[1] cmdArray[2] ... cmdArray[length - 1]
popd dir

Is this the wrong assumption?

user972276
  • 2,973
  • 9
  • 33
  • 46

2 Answers2

0

You are having the permission issue in the C drive.Check by executing "file.exe" manually in the C drive.Better solution will be to keep "file.exe" in some location other than C drive and execute from there.

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • I can execute the file through a cmd prompt with no permission issues – user972276 May 01 '13 at 16:29
  • Right now the file is in the C drive and I have hard coded the path for test purposes. The file should reside anywhere the user has it installed so I need it to work on the C drive if that is really the case. – user972276 May 01 '13 at 16:33
  • But it won't work, if the user has permission issues with that drive! – AllTooSir May 01 '13 at 16:38
  • @NoobUnChained ok, I think the cmd window I was using had admin rights. I opened up a brand new cmd window and ran the command and got this: "Error in Opening Configuration File in Read Mode". So I assume it is a permission issue. How would I fix a permission issue then? – user972276 May 01 '13 at 16:44
  • System administrator should give permission to that particular user account for that file. – AllTooSir May 01 '13 at 16:51
  • @NoobUnChained So, is it not a permission issue? I guess I am a little confused. If it was a permission issue, shouldnt java tell me it is one instead of saying it cant find the file? – user972276 May 01 '13 at 16:53
  • @NoobUnChained so, this is not a permission issue but for some reason, Java cannot actually find the file. I do not know why but if I give the absolute path of the executable as my cmdArray, it works. I dont know why this is. – user972276 May 01 '13 at 17:21
  • @user972276 : just keep the file in other drive , try to execute , and see what happens , mean while , in your c drive , go to the file.exe , open its property , remove read only option and execute again – Hussain Akhtar Wahid 'Ghouri' May 01 '13 at 19:26
0

Perhaps, You will need to include your directory to your path. You may take a look at this question and this tutorial

Community
  • 1
  • 1
lifus
  • 8,074
  • 1
  • 19
  • 24