简体   繁体   English

java getRuntime()。exec()无法正常工作?

[英]java getRuntime().exec() not working?

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. 基本上,当我在终端中手动键入这些命令时,sift程序会运行并写入一个.key文件,但是当我尝试从程序中调用它时,则不会写入任何内容。

Am I using the exec() method correctly? 我是否正确使用exec()方法? I have looked through the API and I can't seem to spot where I went wrong. 我已经浏览了API,但似乎找不到错误的地方。

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

The command line you are using is a DOS command line in the format: 您使用的命令行是以下格式的DOS命令行:

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 a)使用Java处理输入和输出文件

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) 请参阅: 无法从Java Process中读取InputStream(Runtime.getRuntime()。exec()或ProcessBuilder)

b) Use cmd.exe to execute the command as is b)使用cmd.exe按原样执行命令

cmd.exe /c "prog < input > output"

You can't use redirections ( < and > ) with Runtime.exec as they are interpreted and executed by the shell. 您不能在Runtime.exec使用重定向( <> ),因为它们是由Shell解释和执行的。 It only works with one executable and its arguments. 它仅适用于一个可执行文件及其参数。

Further reading: 进一步阅读:

You cannot use input/output redirection with Runtime.exec . 您不能将输入/输出重定向与Runtime.exec On the other hand, the same method returns a Process object, and you can access its input and output streams. 另一方面,相同的方法返回一个Process对象,您可以访问其输入和输出流。

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

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

*For special characters that usually cause problems: This code works correctly even with file names like: "1 - Volume 1 (Fronte).jpg" *对于通常会导致问题的特殊字符:即使文件名如:“ 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} 在Windows 7上测试{guscoder:912081574}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM