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!

4
  • its clearly stating its not able to locate the file in the present working directory Commented Oct 27, 2016 at 0: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.. Commented Oct 27, 2016 at 0: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 Commented Oct 27, 2016 at 0: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: docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html Commented Oct 27, 2016 at 1:28

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

5 Comments

Use the overload that accepts a String[], otherwise you will be unable to process filenames with spaces.
Thank you, could you specify where exactly? String [] command?
@nck: See Runtime; also consider ProcessBuilder, shown here.
@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
@nck: Sadly, without a complete example, there are too many ways for this to go awry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.