Using redirection operators with Java Runtime Exec

后端 未结 1 1085
无人共我
无人共我 2021-01-27 02:50

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

String cmd = encoderFull + \" \" + \"-i \" + originalFull + \" \"+ \"-bitrate\" + \" \" +

相关标签:
1条回答
  • 2021-01-27 03:22

    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.

    0 讨论(0)
提交回复
热议问题