1

Basically, when I type these commands in the terminal by hand, the sift program works and writes a .key file, but when I try to call it from my program, nothing is written.

Am I using the exec() method correctly? I have looked through the API and I can't seem to spot where I went wrong.

public static void main(String[] args) throws IOException, InterruptedException
{ 
 //Task 1: create .key file for the input file
 String[] arr = new String[3];
 arr[0] = "\"C:/Users/Wesley/Documents/cv/final project/ObjectRecognition/sift/siftWin32.exe\"";
 arr[1] = "<\"C:/Users/Wesley/Documents/cv/final project/ObjectRecognition/sift/cover_actual.pgm\"";
 arr[2] = ">\"C:/Users/Wesley/Documents/cv/final project/ObjectRecognition/sift/keys/cover_actual.key\"";
 String command = (arr[0]+" "+arr[1]+" "+arr[2]);
 Process p=Runtime.getRuntime().exec(command); 
 p.waitFor(); 
 BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream())); 
 String line=reader.readLine(); 
 while(line!=null) 
 { 
 System.out.println(line); 
 line=reader.readLine(); 
 } 
}
asked Jul 27, 2012 at 18:38
3
  • what error due you get?? Commented Jul 27, 2012 at 18:40
  • I don't get any errors, but it also doesn't write the .key file like its supposed to. Commented Jul 27, 2012 at 18:43
  • Are you sure you can use output redirection with Runtime.exec? Commented Jul 27, 2012 at 18:43

5 Answers 5

4

The command line you are using is a DOS command line in the format:

prog < input > output

The program itself is executed with no arguments:

prog

However the command from your code is executed as

prog "<" "input" ">" "output"

Possible fixes:

a) Use Java to handle the input and output files

Process process = Runtime.getRuntime().exec(command);
OutputStream stdin = process.getOutputStream();
InputStream stdout = process.getInputStream();
// Start a background thread that writes input file into "stdin" stream
...
// Read the results from "stdout" stream
...

See: Unable to read InputStream from Java Process (Runtime.getRuntime().exec() or ProcessBuilder)

b) Use cmd.exe to execute the command as is

cmd.exe /c "prog < input > output"
answered Jul 27, 2012 at 18:47
Sign up to request clarification or add additional context in comments.

1 Comment

The command is actually equivalent to Runtime.getRuntime().exec("prog", new String[] {"<", "input", ">", "output"})
1

You can't use redirections (< and >) with Runtime.exec as they are interpreted and executed by the shell. It only works with one executable and its arguments.

Further reading:

answered Jul 27, 2012 at 18:44

Comments

0

You cannot use input/output redirection with Runtime.exec. On the other hand, the same method returns a Process object, and you can access its input and output streams.

Process process = Runtime.exec("command here");
// these methods are terribly ill-named:
// getOutputStream returns the process's stdin
// and getInputStream returns the process's stdout
OutputStream stdin = process.getOutputStream();
// write your file in stdin
stdin.write(...);
// now read from stdout
InputStream stdout = process.getInputStream();
stdout.read(...);
answered Jul 27, 2012 at 18:48

2 Comments

This will not work if the command you are using works like a filter by constantly reading from it's standard input and writing to it's standard output at the same time. For larger amounts of data, buffers will be depleted and all writes will block. A correct solution is to use two threads or non blocking I/O to read and write simultaneously.
@anttix, that's the kind of details I would expect people reading this to know about; I merely wanted to point out that those streams exist.
0

I test, it's ok. You can try. Good luck

String cmd = "cmd /c siftWin32 <box.pgm>a.key"; 
Process process = Runtime.getRuntime().exec(cmd);
Brad Larson
170k45 gold badges401 silver badges574 bronze badges
answered May 30, 2013 at 16:13

Comments

0

*For special characters that usually cause problems: This code works correctly even with file names like: "1 - Volume 1 (Fronte).jpg"

String strArr[] = {"cmd", "/C", file.getCanonicalPath()};
Process p = rtObj.exec(strArr);///strCmd);

Agree too, redirection not supported here.

Tested on Windows 7 {guscoder:912081574}

Linga
10.6k10 gold badges57 silver badges110 bronze badges
answered Feb 7, 2014 at 10:21

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.