【问题标题】:ProcessBuilder environment variable in javaJava中的ProcessBuilder环境变量
【发布时间】:2012-06-25 23:09:09
【问题描述】:

我正在尝试为 ProcessBuilder 对象添加环境变量,但是当我在 ProcessBuilder 中调用该新变量时出现错误。 这就是我构建流程的方式

public class OTU
{
    public static void main(String[] args) throws Exception
    {
        ProcessBuilder pb = new ProcessBuilder();
        Map<String, String> env = pb.environment();
        //set environment variable u
        env.put("u", "util/");

        pb.command("echo $u");
        Process p = pb.start();
        String output = loadStream(p.getInputStream());
        String error  = loadStream(p.getErrorStream());
        int rc = p.waitFor();
        System.out.println("Process ended with rc=" + rc);
        System.out.println("\nStandard Output:\n");
        System.out.println(output);
        System.out.println("\nStandard Error:\n");
        System.out.println(error);
    }

    private static String loadStream(InputStream s) throws Exception
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(s));
        StringBuilder sb = new StringBuilder();
        String line;
        while((line=br.readLine()) != null)
            sb.append(line).append("\n");
        return sb.toString();
    }
}

我得到了错误

Exception in thread "main" java.io.IOException: Cannot run program "$u": java.io.IOException: error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:475)
    at ca.utoronto.siseq_1_2.OTU.main(OTU.java:22)
Caused by: java.io.IOException: java.io.IOException: error=2, No such file or directory
    at java.lang.UNIXProcess.<init>(UNIXProcess.java:164)
    at java.lang.ProcessImpl.start(ProcessImpl.java:81)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:468)
    ... 1 more

如果我只是为此进程设置变量,我不明白为什么会出现错误。 请帮助我如何设置环境变量,以便我可以在 ProcessBuilder 中使用它。

【问题讨论】:

    标签: java unix processbuilder


    【解决方案1】:

    Alfredo O 的示例为您提供了正确的想法。您需要告诉 ProcessBuilder 使用什么程序来执行您的命令。在这种情况下,bash with the "-c" switch 告诉 bash 将接下来的内容(即“echo $u”)解释为命令。

    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.Map;
    
    public class OTU {
        public static void main(String[] args) throws Exception {
            ProcessBuilder pb = new ProcessBuilder("/bin/bash", "-c", "echo $u");
            Map<String, String> env = pb.environment();
            // set environment variable u
            env.put("u", "util/");
    
            Process p = pb.start();
            String output = loadStream(p.getInputStream());
            String error = loadStream(p.getErrorStream());
            int rc = p.waitFor();
            System.out.println("Process ended with rc=" + rc);
            System.out.println("\nStandard Output:\n");
            System.out.println(output);
            System.out.println("\nStandard Error:\n");
            System.out.println(error);
        }
    
        private static String loadStream(InputStream s) throws Exception {
            BufferedReader br = new BufferedReader(new InputStreamReader(s));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null)
                sb.append(line).append("\n");
            return sb.toString();
        }
    }
    

    这会产生以下输出:

    Process ended with rc=0
    
    Standard Output:
    
    util/
    
    
    Standard Error:
    

    【讨论】:

    • windows 平台前缀:cmd /c ... unix 平台前缀:sh -c ...
    【解决方案2】:

    这在 Windows 中适用于我:

    @Test
    public void testProcessBuilder() throws IOException {
        ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/C", "echo Hello %name%");
        Map<String, String> environment = processBuilder.environment();
        environment.put("name", "Alfredo Osorio");
        Process p = processBuilder.start();
        String line;
        BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
        while ((line = r.readLine()) != null) {
            System.out.println(line);
        }
        r.close();
    }
    

    输出:

    你好阿尔弗雷多·奥索里奥

    正如您在 Windows 中看到的,您使用 %environmentVariable% 而不是 $environementVariable

    【讨论】:

    • 有没有办法让这个跨操作系统?
    • +1 为他指明了正确的方向,尽管没有解释它为什么起作用 - 你已经告诉 Java 执行 cmd.exe。
    猜你喜欢
    • 2015-08-14
    • 2015-01-15
    • 1970-01-01
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    • 2013-04-03
    • 2016-08-11
    • 1970-01-01
    相关资源
    最近更新 更多