在Java Runtime Exec中使用重定向运算符 [英] Using redirection operators with Java Runtime Exec

查看:84
本文介绍了在Java Runtime Exec中使用重定向运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个字符串,我在Windows XP的JVM中使用Runtime.exec()。



String cmd = encoderFull + + - i+ originalFull ++-bitrate++ bitrateVBR [i] ++ - bvhq++ - avi+ outputFull ++2>& 1++1>+ outputFile +。txt;



不幸的是'2>& 1 1>输出.txt'不起作用。其余的都没问题。有没有办法使用这些命令来创建这个文件?



再见,
David

解决方案

基本上,没有 - Runtime.exec()不支持你要做的事情。



注意运行时。 exec是一个shell - 对于你明显使用的exec(String),它会对字符串进行标记(以空格作为分隔符),并尝试将第一个标记作为程序执行,其余的令牌作为该程序的参数。



因此,在您的情况下,重定向运算符将作为参数发送到'encoderFull命名的应用程序'变量 - 它们不会像你期望的那样由shell解释,因为没有shell。



据我所知,你有两个选择:


  1. 编写批处理文件(对于Windows)或shell脚本(对于* nix),它执行您的应用程序并执行重定向。然后使用Runtime.exec()


  2. <使用批处理文件/ shell脚本作为参数调用shell(cmd或sh,或者你有什么) p>从Runtime.exec创建的进程中读取输出流,并使用java io将内容写入相应的文件


我强烈推荐#1赞成#2



您可能还应该阅读当Runtime.exec()不会 - 浏览与Runtime.exec()方法相关的陷阱再往前走。


I have this String which I used with Runtime.exec() in JVM in Windows XP.

String cmd = encoderFull + " " + "-i " + originalFull + " "+ "-bitrate" + " " + bitrateVBR[i] + " " + "-bvhq"+ " " + "-avi " + outputFull + " " + "2>&1" + " " + "1>"+ outputFile + ".txt";

Unoftunately '2>&1 1>output.txt' doesn't work. The rest is ok. Is there a way to use the commands so this file gets created?

Bye, David

解决方案

Basically, no - what you are trying to do is not supported by Runtime.exec().

Note that Runtime.exec is not a shell - in the case of exec(String), which you are apparently using, it tokenizes the string (with whitespace as the separator), and attempts to execute the first token as a program, with the rest of the tokens as arguments to that program.

So, in your case, the redirection operators will be sent as parameters to the application named by the 'encoderFull' variable - they will not be interpreted by the shell as you expect, since there is no shell.

As far as I know, you have two choices here:

  1. Write a batch file (for Windows) or a shell script (for *nix), that executes your application and does the redirection. Then invoke the shell (cmd or sh, or what-have-you), with the batch file / shell script as an argument, using Runtime.exec()

  2. Read the output stream from the process created by Runtime.exec and write the content to the appropriate file using java io

I highly recommend #1 in favor of #2

You should probably also read this article on "When Runtime.exec() won't - Navigate yourself around pitfalls related to the Runtime.exec() method" before going any farther.

这篇关于在Java Runtime Exec中使用重定向运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆