简体   繁体   English

Java流程构建器和使用环境变量

[英]Java processbuilder and using environment variables

What I want to do is I want to run a process, however because this process itself relies on environment variables, directly calling it causes error within the process. 我想要做的是我想运行一个进程,但是因为这个进程本身依赖于环境变量,所以直接调用它会导致进程内出错。 For those who are wondering what this is, it's rake tool. 对于那些想知道这是什么的人来说,它是rake工具。 For this reason I thought maybe it's better to use bash and using it through bash would eliminate the issue. 出于这个原因,我想也许最好使用bash并通过bash使用它可以消除这个问题。 However that doesn't seem to be the case. 然而,情况似乎并非如此。

Here is my code: 这是我的代码:

public static void runPB(String directory) throws IOException {
        ProcessBuilder processBuilder = new ProcessBuilder(
                "/bin/bash");
        processBuilder.directory(new File(directory));
        Process process = processBuilder.start();
        OutputStreamWriter osw = new OutputStreamWriter(process.getOutputStream());
        osw.write("rake routes");
        osw.close();
        printStream(process.getErrorStream());
        printStream(process.getInputStream());
    }

    public static void printStream(InputStream is) throws IOException {
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
    }

I know it is environment related issue because the error that I am getting is described here cannot load such file -- bundler/setup (LoadError) 我知道这是与环境相关的问题,因为我在这里描述的错误无法加载这样的文件 - bundler / setup(LoadError)

Further I checked processBuilder.environment() returns less environment variables than entering env . 此外,我检查了processBuilder.environment()返回的环境变量少于输入env I went ahead and changed the osw.write() line and tried echo $GEM_HOME there, which doesn't print anything and if I do this on my OSs bash then I get the path, I also tried other common things like echo $SHELL and it prints the shell location in both Java code and in bash. 我继续改变了osw.write()行并尝试了echo $GEM_HOME ,它没有打印任何内容,如果我在我的操作系统上执行此操作,那么我得到了路径,我还尝试了其他常见的东西,如echo $SHELL它在Java代码和bash中打印shell位置。

So my questions are: 所以我的问题是:

1) Why is my operating system's environment variables are different than the ProcessBuilder.environment() method? 1)为什么我的操作系统的环境变量与ProcessBuilder.environment()方法不同?

2) Does Process class consider using environment variables that were given out by ProcessBuilder.environment() ? 2) Process类是否考虑使用ProcessBuilder.environment()发出的环境变量? If so then how can we add the missing ones from the operating system's level? 如果是这样,那么我们如何从操作系统的级别添加缺少的?

1) The varaibles you see in your java process are those inheritd from the process you started the java process from. 1)您在java进程中看到的变量是从您启动java进程的进程继承的变量。 Ie If you launch it from a shell it should have the same variables as the shell had. 即如果从shell启动它,它应该具有与shell相同的变量。 You need to investigate which variables are actually set before launching your Java application and why the ones you expect are not set in that context. 您需要在启动Java应用程序之前调查实际设置的变量,以及为什么未在该上下文中设置您期望的变量。

To answer part 2, yes, the process will be launched with the environment in ProcessBuilder.environment() . 要回答第2部分,是的,将在ProcessBuilder.environment()使用环境启动该过程。 You can simply add things to the map returned by ProcessBuilder.environment() , that will extend the runtime environment: 您可以简单地将事物添加到ProcessBuilder.environment()返回的映射中,这将扩展运行时环境:

ProcessBuilder pb = new ProcessBuilder("foo");
pb.environment().put("MY_VAR", "foobar");

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

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