Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ProcessBuilder environment variable in java

I'm trying to add a environment variable for a ProcessBuilder object but then when I call on that new variable in the ProcessBuilder I get an error. this is how I build the Process

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();
    }
}

I get the error

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

I dont understand why I am getting the error if I just set the variable for this Process. Please help me how to set the env variable so I can use it in the ProcessBuilder.

like image 738
Julio Diaz Avatar asked Jun 25 '12 23:06

Julio Diaz


People also ask

What is ProcessBuilder in Java?

This class is used to create operating system processes. Each ProcessBuilder instance manages a collection of process attributes. The start() method creates a new Process instance with those attributes.

What is environmental variable in Java?

Like properties in the Java platform, environment variables are key/value pairs, where both the key and the value are strings. The conventions for setting and using environment variables vary between operating systems, and also between command line interpreters.

Where are Java environment variables stored?

Environment variables are set in the OS, e.g. in Linux export HOME=/Users/myusername or on Windows SET WINDIR=C:\Windows etc, and, unlike properties, may not be set at runtime. To get a specific environment variable you can use System.


2 Answers

This works for me in 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();
}

Output:

Hello Alfredo Osorio

As you can see in Windows you use the %environmentVariable% instead of the $environementVariable

like image 25
Alfredo Osorio Avatar answered Oct 17 '22 13:10

Alfredo Osorio


Alfredo O's example gives you the right idea. You need to tell the ProcessBuilder what program to use to execute your command. In this case bash with the "-c" switch, which tells bash to interpret what comes next (i.e. "echo $u") as a command.

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();
    }
}

This produces the following output:

Process ended with rc=0

Standard Output:

util/


Standard Error:
like image 76
orangepips Avatar answered Oct 17 '22 12:10

orangepips