Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Runtime.exec() Does Not Respect PATH in envp

807589Jul 15 2008 — edited Jul 15 2008
Hi all,

I am trying to launch a program that is located outside of the usual system path. There is a script that updates the PATH variables with the location of this program, which I am using inside of Java to create an array of environment variables to pass to Runtime.exec(). This part works quite well (Runtime.exec("env",environmentarray) prints out the correct values), but for some reason Runtime.exec() ignores the PATH variables inside of it.

To try to solve this, I simplified the code into the following (Note: UberStreamBuffer is just a class that buffers the output of the program):
public class Main 
{
        public static void exec(String cmd,String[] env)
        {
                Runtime rt=Runtime.getRuntime();

                UberStreamBuffer stdout=null;
                Process p=null;
                try {
                        p=rt.exec(cmd,env);

                        stdout=new UberStreamBuffer(p.getInputStream());
                        stdout.start();

                } catch (IOException e) {
                        e.printStackTrace();
                        return;
                }

                try {
                        p.waitFor();
                } catch (InterruptedException e) {
                        e.printStackTrace();
                }
                System.out.println(stdout.getData());

        }

        public static void main(String[] args) 
        {
                String[] env={"PATH=/home/kka34:/bin:/usr/bin"};

                exec("env",env);
                exec("karol",env);
        }
}
Here I created a bash script called karol that prints out "Hello World!", placed it into /home/kka34 and given it executable permissions. But when I run the java program, I get the following output:
PATH=/home/kka34:/bin:/usr/bin

java.io.IOException: java.io.IOException: karol: not found
        at java.lang.UNIXProcess.<init>(UNIXProcess.java:148)
        at java.lang.ProcessImpl.start(ProcessImpl.java:65)
        at java.lang.ProcessBuilder.start(ProcessBuilder.java:451)
        at java.lang.Runtime.exec(Runtime.java:591)
        at java.lang.Runtime.exec(Runtime.java:429)
        at java.lang.Runtime.exec(Runtime.java:367)
        at Main.exec(Main.java:13)
        at Main.main(Main.java:42)
As you can see, it cannot find the program even though the environment variables for exec are correctly set. However if I place the script into /home/kka34/bin (and do not update the PATH!), everything works. So my conclusion is that Runtime.exec() ignores changes to the PATH variable inside of the environment array passed to it.

My question is: Is this conclusion correct and if so, how could I get around it? If not, do you have any ideas about what the problem could be?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 12 2008
Added on Jul 15 2008
3 comments
752 views