java getRuntime().exec()不工作?

java getRuntime().exec()不工作?,java,runtime,exec,Java,Runtime,Exec,基本上,当我输入这些命令时 sift程序手工编写终端,并编写一个.key文件,但当我试图从程序调用它时,什么也没写 我是否正确使用exec()方法?我已经浏览了API,但似乎看不出哪里出了问题 public static void main(String[] args) throws IOException, InterruptedException { //Task 1: create .key file for the input file

基本上,当我输入这些命令时 sift程序手工编写终端,并编写一个.key文件,但当我试图从程序调用它时,什么也没写

我是否正确使用exec()方法?我已经浏览了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(); 
        } 
}
publicstaticvoidmain(String[]args)抛出IOException、interruptedeexception
{           
//任务1:为输入文件创建.key文件
字符串[]arr=新字符串[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/keys/cover\u actual.key\”;
字符串命令=(arr[0]+“”+arr[1]+“”+arr[2]);
进程p=Runtime.getRuntime().exec(命令);
p、 waitFor();
BufferedReader=新的BufferedReader(新的InputStreamReader(p.getInputStream());
字符串行=reader.readLine();
while(行!=null)
{ 
系统输出打印项次(行);
line=reader.readLine();
} 
}

您不能将重定向(
)与
Runtime.exec一起使用,因为它们是由shell解释和执行的。它只适用于一个可执行文件及其参数

进一步阅读:


您不能将重定向(
)与
Runtime.exec一起使用,因为它们是由shell解释和执行的。它只适用于一个可执行文件及其参数

进一步阅读:


您使用的命令行是DOS命令行,格式如下:

prog < input > output
但是,代码中的命令是按如下方式执行的

prog "<" "input" ">" "output"
见:

b) 使用cmd.exe按原样执行命令

cmd.exe /c "prog < input > output"
cmd.exe/c“progoutput”

您使用的命令行是DOS命令行,格式如下:

prog < input > output
但是,代码中的命令是按如下方式执行的

prog "<" "input" ">" "output"
见:

b) 使用cmd.exe按原样执行命令

cmd.exe /c "prog < input > output"
cmd.exe/c“progoutput”

不能将输入/输出重定向与
Runtime.exec一起使用。另一方面,相同的方法返回一个
进程
对象,您可以访问它的输入和输出流

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

您不能将输入/输出重定向与
Runtime.exec
一起使用。另一方面,相同的方法返回一个
进程
对象,您可以访问它的输入和输出流

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

我测试一下,没关系。你可以试试。祝你好运

String cmd = "cmd /c siftWin32 <box.pgm>a.key"; 
Process process = Runtime.getRuntime().exec(cmd);
String cmd=“cmd/c siftWin32 a.key”;
processprocess=Runtime.getRuntime().exec(cmd);

我测试一下,没问题。你可以试试。祝你好运

String cmd = "cmd /c siftWin32 <box.pgm>a.key"; 
Process process = Runtime.getRuntime().exec(cmd);
String cmd=“cmd/c siftWin32 a.key”;
processprocess=Runtime.getRuntime().exec(cmd);

*对于通常会导致问题的特殊字符: 此代码即使在文件名为“1-Volume 1(Fronte).jpg”的情况下也能正常工作

也同意,此处不支持重定向

在Windows7上测试
{guscoder:912081574}

*对于通常会导致问题的特殊字符: 此代码即使在文件名为“1-Volume 1(Fronte).jpg”的情况下也能正常工作

也同意,此处不支持重定向

在Windows7上测试
{guscoder:912081574}

您会遇到什么错误?我没有遇到任何错误,但它也没有像预期的那样编写.key文件。您确定可以将输出重定向与
Runtime.exec
一起使用吗?您会遇到什么错误?我没有遇到任何错误,但它也不像预期的那样写入.key文件。您确定可以将输出重定向与
Runtime.exec
一起使用吗?如果您正在使用的命令像过滤器一样工作,不断读取其标准输入,同时写入其标准输出,则这将不起作用。对于较大的数据量,缓冲区将耗尽,所有写操作都将阻塞。正确的解决方案是使用两个线程或非阻塞I/O同时读写。@anttix,这是我希望阅读本文的人了解的细节;我只是想指出,这些流是存在的。如果您正在使用的命令像过滤器一样工作,不断地读取标准输入,同时写入标准输出,那么这将不起作用。对于较大的数据量,缓冲区将耗尽,所有写操作都将阻塞。正确的解决方案是使用两个线程或非阻塞I/O同时读写。@anttix,这是我希望阅读本文的人了解的细节;我只想指出那些流是存在的。这个命令实际上相当于
Runtime.getRuntime().exec(“prog”,新字符串[]{”,“output”})
这个命令实际上相当于
Runtime.getRuntime().exec(“prog”,新字符串[]{”,“output”})