1

I'm trying to run a simple prompt command froma java application.

No matter what I try, I alwasy receive a CreateProcess error=2.

This is my code:

File f = new File("C:/Users/my/path/to/the/executable/");
String[] commands = { "text.txt", "--command1" /* others commands */ };
// omitting try/catch
Process p = Runtime.getRuntime().exec("myprogram.exe", commands, f);
p.waitFor();
// other code

The exe takes as first parameter a .txt file, then it takes normal commands as --command1.

How can I get this working? Thanks in advance!

2
  • What error do you have? Commented Oct 22, 2016 at 10:20
  • @talex, more specifically it is: Java.io.IOException: Cannot run program "myprogram.exe" (in directory "/thedirectory"): error=2, No such file or directory Commented Oct 22, 2016 at 10:23

1 Answer 1

1

The directory parameter is not the path to the executable, it is the working directory, so if you are on command line, it would be the current directory you are in when you launch the command.

You need to specify the whole path in first parameter, or add it to the PATH environment variable

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! The error is gone but there's a new problem. the same command produce output if I run it manually in cmd but return nothing if launched from java. What could it be?
You need a BufferReader object to read the output from the cmd....something like: BufferedReader bf = new BufferedReader(new InputStreamReader(p.getInputStream())); Since your not showing your "other code" there's no way of knowing if you have this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.