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(); 
        } 
}
wesley.ireland
  • 643
  • 3
  • 12
  • 21

5 Answers5

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"
Community
  • 1
  • 1
anttix
  • 7,709
  • 1
  • 24
  • 25
  • The command is actually equivalent to `Runtime.getRuntime().exec("prog", new String[] {"<", "input", ">", "output"})` – Gumbo Feb 08 '13 at 22:15
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:

Community
  • 1
  • 1
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
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(...);
zneak
  • 134,922
  • 42
  • 253
  • 328
  • 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 Jul 27 '12 at 19:01
  • @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. – zneak Jul 27 '12 at 20:19
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
  • 170,088
  • 45
  • 397
  • 571
thanhbuu
  • 1
  • 1
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,379
  • 10
  • 52
  • 104
Name
  • 1