java getRuntime().exec() not working?

浪尽此生 提交于 2019-12-08 06:17:09

问题


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(); 
        } 
}

回答1:


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"



回答2:


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:

  • https://stackoverflow.com/a/11250789/105224



回答3:


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(...);



回答4:


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);



回答5:


*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}



来源:https://stackoverflow.com/questions/11693748/java-getruntime-exec-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!