简体   繁体   English

为什么同一命令在bash脚本中有效,而在Java Runtime中却没有?

[英]Why does the same command work in a bash script but not in Java Runtime directly?

The command "cat ~/desktop/b.mpg ~/desktop/b2.mpg > ~desktop/intermediate_all.mpg" does not seem to work via Java Runtime alone (as seen in the example below); 命令“cat~ / desktop / b.mpg~ / desktop / b2.mpg> ~desktop / intermediate_all.mpg”似乎无法单独通过Java Runtime工作(如下例所示);

public class Test {
    public static void main(final String[] args)  {
        String[] cmd = {"cat ~/desktop/b.mpg ~/desktop/b2.mpg > ~desktop/intermediate_all.mpg"};
        try { Runtime.getRuntime().exec(cmd);  }
        catch (IOException e) { e.printStackTrace();}
    }
}


However, when put into a .sh file like in this second example it works just fine.... 但是,当像第二个例子一样放入.sh文件时它工作得很好....

public class Test {
    public static void main(final String[] args)  {
        try { Runtime.getRuntime().exec("/users/nn/desktop/configure.sh"); }
        catch (IOException e) { e.printStackTrace();}
    }
}


在此输入图像描述

Can anybody please tell me what the fundamental process is being lost when moving from a bash script to straight Java Runtime? 任何人都可以告诉我从bash脚本转向直接Java Runtime时基本过程丢失了什么? FYI, I am using OSX, have already tried using absolute filepaths, and know about Process Builder (which has the same effect) is preferred to using Java Runtim--as has been stated a thousand times on this forum already, so lets avoid beating the dead horse on that one. 仅供参考,我正在使用OSX,已经尝试过使用绝对文件路径,并且了解Process Builder(具有相同的效果)比使用Java Runtim更受欢迎 - 正如在此论坛上已经说过一千次,所以让我们避免挨打那个死马。

Thanks 谢谢

The command being executed is cat with arguments. 正在执行的命令是带参数的cat The command and its arguments must be separate elements of the array. 该命令及其参数必须是数组的单独元素。

Also, you can't redirect using Runtime.exec() - you must use ProcessBuilder : 此外,您无法使用Runtime.exec()重定向 - 您必须使用ProcessBuilder

Try this: 尝试这个:

ProcessBuilder pb = new ProcessBuilder("cat", "~/desktop/b.mpg", "~/desktop/b2.mpg");
pb.redirectOutput(new File("~/desktop/intermediate_all.mpg"));
Process p = pb.start();

It is likely that the shell location ~ will not be understood, so you may have to use the full absolute path for the files 可能无法理解shell位置~ ,因此您可能必须使用文件的完整绝对路径

Try this: 尝试这个:

Runtime.getRuntime().exec(new String[] {
    "/bin/bash", "-c", 
    "cat ~/desktop/b.mpg ~/desktop/b2.mpg > ~/desktop/intermediate_all.mpg" })

Because in the second case, you are effectively running "bash", "-c", "cat etc >file" where Bash takes care of parsing the redirection for you. 因为在第二种情况下,您实际上正在运行"bash", "-c", "cat etc >file" ,其中Bash负责为您解析重定向。 Redirection is a feature of the shell, not of cat ; 重定向是shell的一个特性,而不是cat ; if you run the raw process without a shell, the features of the shell are not available to you. 如果在没有shell的情况下运行原始进程,则无法使用shell的功能。

在您的java代码〜/ desktop / b.mpg~/ desktop / b2.mpg> ~desktop / intermediate_all.mpg中,您必须在>〜/ desktop / intermediate_all.mpg之后给出完整路径

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

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