1

When I run a bach script containing "echo $Path" command what it outputs when run by java is different from what it outputs when run from command line. It also affects other commands of my script. Why is this happening and how do I avoid?

Following is my function to run a bashscript

public static String executeCommands(File tempScript, Boolean deleteFile)
        throws IOException, InterruptedException {

    StringBuffer output = new StringBuffer();

    try {
        ProcessBuilder pb = new ProcessBuilder("bash", tempScript.toString());
        pb.inheritIO();
        Process process = pb.start();
        process.waitFor();


        BufferedReader reader =
                new BufferedReader(new InputStreamReader(process.getInputStream()));

        String line = "";
        while ((line = reader.readLine()) != null) {
            output.append(line + "\n");
        }
        return line;

    } finally {
        if (deleteFile == true)
            tempScript.delete();
    }
}

when the script contains "echo $PATH" in bashscript

output is

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

But when I run from commandline output is

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/scala/scala-2.11.8/bin:/Users/<user>/Installations/activator-dist-1.3.10
4
  • 2
    Call bash with -l. Commented Oct 21, 2016 at 3:54
  • That is they way they are supposed to work. It is to be expected. Also stop running as root, it is a bad habit to fall into. Commented Oct 21, 2016 at 4:07
  • I was running this test from a unit test and I ran the unit test from eclipse. Why do you think it was running under root? Commented Oct 21, 2016 at 4:56
  • The paths of /usr/sbin and /sbin mean secure binaries. Those should be limited to elevated users, by default only root. You have those in your path. Commented Oct 21, 2016 at 15:11

2 Answers 2

2

When we run the command from terminal it reads the environment variables from .bashrc file, but it seems eclipse does not read environment variables from .bashrc.

launch eclipse with ./eclipse -DPATH=$PATH to read from bashrc

PATH variable from

1.terminal

user@ubuntu:~$ javac SS47.java 
user@ubuntu:~$ java SS47 
/home/user/perl5/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/user/apache-maven-3.3.3/bin:/home/user/apache-maven-3.3.3/bin:/opt/jdk/jdk1.8.0_60/bin:/opt/jdk/jdk1.8.0_60/jre/bin:/home/user/dsc-cassandra-2.1.6/bin:/home/user/hadoop-2.6.0/bin:/home/user/hadoop-2.6.0/sbin:/home/user/android/android-studio/bin:/home/user/android/android-sdk-linux/platform-tools:/home/user/elasticsearch-2.3.5/bin:/home/user/scala-2.11.8/bin::/home/user/apache-maven-3.3.3/bin

2.eclipse with out $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

3.eclipse with PATH ./eclipse -DPATH=$PATH

/home/user/perl5/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/user/apache-maven-3.3.3/bin:/home/user/apache-maven-3.3.3/bin:/opt/jdk/jdk1.8.0_60/bin:/opt/jdk/jdk1.8.0_60/jre/bin:/home/user/dsc-cassandra-2.1.6/bin:/home/user/hadoop-2.6.0/bin:/home/user/hadoop-2.6.0/sbin:/home/user/android/android-studio/bin:/home/user/android/android-sdk-linux/platform-tools:/home/user/elasticsearch-2.3.5/bin:/home/user/scala-2.11.8/bin::/home/user/apache-maven-3.3.3/bin
Sign up to request clarification or add additional context in comments.

4 Comments

I normally run eclipse from applications folder in mac. Is there any config I can change inside eclipse for it to read from bashrc also?
try launching from terminal ./eclipse -DPATH=$PATH am using Ubuntu
or check if you have /etc/environment file, eclipse reads envs form this file during initialization, append $PATH in environment file, restart eclipse
or you can add VM arguments in eclipse.ini file
-1

As Elliott-Frisch pointed out in his comment, just call bash with the option -l which

Make[s] bash act as if it had been invoked as a login shell (see INVOCATION below). Source

This way you don't have to call Eclipse with different launch options etc. and it makes your solution independent, being able to be run without the non customized Eclipse or just bare-bone JRE.

So just edit your call ProcessBuilder like this:

ProcessBuilder pb = new ProcessBuilder("bash", "-l", tempScript.toString());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.