0

I am running a Java program on Unix (AIX to be specific) and need to call an external shell script. Additionally, I need to set some environment variables that will be available to the shell script (technically, they need to be avaiable to an Ant program that the shell script invokes). I know this setup sounds strange but my Java program is an installation program that wraps a pre-existing Ant script. I want to use it to prompt for some passwords to feed into the Ant script as environment variables as encrypted values.

My problem is that the environment variables I add are not visible to the shell script and, by extension, my Ant script. I am using the ProcessBuilder.enviroment() map to set this as follows:

ProcessBuilder pb = new ProcessBuilder("build.sh", "install");

pb.directory("app_root/install"));

//if we have encrypted passwords, set them as environment variables on the child process
if (this.encryptedPasswords.size() > 0)
{
    for (Entry<String,String> entry : this.encryptedPasswords.entrySet())
    {
        String userName = entry.getKey();
        String encryptedPassword = entry.getValue();

        if (this.debug == true)
            System.out.println("Adding environment variable [" + userName + "] with value [" + encryptedPassword + "]");

        pb.environment().put(userName, encryptedPassword);
    }
}

My shell script (which I cannot easily change) basically looks like this:

#!/bin/sh

. ./build.env.sh

ant -buildfile build_impl.xml $*

The build.env.sh script sets and export some environment variables like WEBLOGIC_HOME, etc. that are static and also used by the Ant script.

This same concept has worked on Windows. I am sure it is something with Unix/AIX that I am not familiar with. For instance, do I need to be exported these new environment variables? If so, how would that be done from ProcessBuilder in Java?

Thanks in advance.

  • https://stackoverflow.com/questions/318239/how-do-i-set-environment-variables-from-java this might help – AprojectX Feb 12 '18 at 23:12
  • Thanks- I had seen that post which references some hacks to change your own process's environment variables. However, I don't need to do that. I simply need to pass new environment variables to a process that I am forking from Java. It is just not working for me on AIX. – Ned Miles Feb 13 '18 at 00:25

1 Answers1

0

If the process ask for password every time and used by the JVM, you can add from java using System.setProperty(..). This essentially same as providing an -D option for jvm argument.

Instead of

pb.environment().put(userName, encryptedPassword);

can write

    System.setProperty("userName",userName);
    System.setProperty("encryptedPassword", encryptedPassword);
Midhun
  • 146
  • 1
  • 4
  • Thanks. Unfortunately, I am not calling a JVM (or even Ant) directly and have to go through this shell script, which I cannot easily change. Therefore, my plan was to use environment variables an have them propagate down to the child processes. – Ned Miles Feb 12 '18 at 23:41
  • Are you adding the host machine environment variable? – Midhun Feb 12 '18 at 23:52
  • Not sure exactly what this means but if you mean do the new environment variable names conflict with existing environment variables, the answer is no. – Ned Miles Feb 13 '18 at 00:33
  • You can do something like - ProcessBuilder pb = new ProcessBuilder("build.sh", "-c", "echo $username"); Map env = pb.environment(); env.put("username", value); – Midhun Feb 13 '18 at 01:00