1

I have a C code which I have compiled and added to path in order to be able to execute it form anywhere (I've double checked that I can do that)

Now I want to do a GUI to work with it in an easier way. I ask the user to input a file and an output directory.

In a click button I put the code to execute the command from the GUI:

String command = "myprogram -e " + file;
new ExecuteShellInstruction().main(command,jTextOutputDirectory.getText());

I execute the code in other class:

p = Runtime.getRuntime().exec(command, null, new File(directory));

But I always get this error:

java.io.IOException: Cannot run program "myprogram" (in directory "/Users/user_name/Documents/folder/example"): error=2, No such file or directory

I've checked that if I write exactly the same from the same folder there is no problem.

Any idea of what I'm doing worng?, If obtained this way of doing it from a question which was marked as correct, maybe I'm missing something, but I've already been 1 hour trying things and nothing seems to work.

Thank you!

nck
  • 1,673
  • 16
  • 40
  • its clearly stating its not able to locate the file in the present working directory – Shreyas Sarvothama Oct 27 '16 at 00:41
  • @ShreyasSarvothama yes, it says so. But as I've mentioned when I do it directly in that directory from the shell it works perfectly.. – nck Oct 27 '16 at 00:52
  • but where are you working your java code from.. set the directory first and then run it.. shell default directory is different and java working directory is different – Shreyas Sarvothama Oct 27 '16 at 00:58
  • @ShreyasSarvothama Sorry, I don't understand what you mean. As I understand with getRuntime().exec method, I specify as parameters the shell instruction, null, and in which folder do I want it to be executed: https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html – nck Oct 27 '16 at 01:28

1 Answers1

1

Finally I found a solution. It seems like you have to tell that your application can be executed by adding "./" at the beginning. Something like this:

String command = "./myprogram -e " + file;
nck
  • 1,673
  • 16
  • 40
  • Use the overload that accepts a `String[]`, otherwise you will be unable to process filenames with spaces. – that other guy Oct 27 '16 at 03:12
  • Thank you, could you specify where exactly? String [] command? – nck Oct 27 '16 at 12:18
  • @nck: See [`Runtime`](https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html); also consider `ProcessBuilder`, shown [here](http://stackoverflow.com/a/20603012/230513). – trashgod Nov 10 '16 at 21:55
  • @trashgod I've tried to do it in the way its in your link but it seems like I'm now in more trouble ':/ [problem](http://stackoverflow.com/questions/40565846/wait-for-a-background-process-to-end-with-swingworker-from-a-different-class) – nck Nov 13 '16 at 15:19
  • @nck: Sadly, without a complete example, there are too many ways for this to go awry. – trashgod Nov 13 '16 at 15:33