在 Java 中使用 Runtime.exec()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1319286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 08:11:54  来源:igfitidea点击:

using Runtime.exec() in Java

javapathexec

提问by Jason S

What do you have to do in Java to get the Runtime.exec() to run a program that is on the path? I'm trying to run gpsbabel which I have put into the path (/usr/local/bin).

你必须在 Java 中做什么才能让 Runtime.exec() 运行路径上的程序?我正在尝试运行已放入路径 (/usr/local/bin) 中的 gpsbabel。

public class GpxLib {

    public static void main(String[] args) {
        try
        {
            Runtime r = Runtime.getRuntime();
            Process p = r.exec("gpsbabel -i garmin -f usb: -o gpx -F -");
            InputStream is = p.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            while (true)
            {
                String s = br.readLine();
                if (s == null)
                    break;
                System.out.println(s);
            }
            br.readLine();
        } catch (IOException e) {
            e.printStackTrace(System.err);
        }
    }
}

采纳答案by Brian Agnew

It will inherit the path from the Java process. So whatever environment the Java process has, the spawned process will have as well. Here's how to check the environment:

它将继承 Java 进程的路径。因此,无论 Java 进程具有什么环境,生成的进程也将具有。以下是检查环境的方法:

Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
     System.out.format("%s=%s%n", envName, env.get(envName));
}

Have you set the PATHandexported it ? If you don't export it, then it's not available to subprocesses.

你设置PATH导出了吗?如果您不导出它,则它不可用于子流程。

Additionally, you mustconsume stdout and stderr concurrently, to prevent blocking. Otherwise stuff will work in some circumstances, then your spawned process will output a different quantity of data and everything will grind to a halt.

此外,您必须同时使用 stdout 和 stderr,以防止阻塞。否则在某些情况下东西会起作用,那么你产生的进程将输出不同数量的数据,一切都会停止。

See this answerfor more details.

有关更多详细信息,请参阅此答案

回答by duffymo

Whatever you do, you must read this. No one should use Runtime.exec()without having read it.

无论你做什么,你都必须阅读这篇文章Runtime.exec()未经阅读,任何人都不应使用。

回答by Jason S

I added a call to System.out.println(System.getenv("PATH"));which only prints out

我添加了一个System.out.println(System.getenv("PATH"));只打印出来的调用

/usr/bin:/bin:/usr/sbin:/sbin

so for some reason /usr/local/bin doesn't show up. Looks like this is a MacOSX question or an Eclipse question, not a Java question. edit:asked this question on superuserinstead.

所以出于某种原因 /usr/local/bin 没有出现。看起来这是一个 MacOSX 问题或一个 Eclipse 问题,而不是一个 Java 问题。编辑:改为在超级用户上这个问题

回答by DareDevil

Here is the solution:

这是解决方案:

ProcessBuilder proc = new ProcessBuilder("<Directory PAth>" + "Executable.exe");
proc.redirectOutput(ProcessBuilder.Redirect.INHERIT);
proc.directory(fi); //fi = the output directory path
proc.start();

is the path where program\application's excutable is located e.g "C:\MyProg\"

是程序\应用程序的可执行文件所在的路径,例如“C:\MyProg\”