How to convert environment variable map from getenv to string array for of name=value

I will use exec in my Java code but I need to get the environment first because exec requires the env as the second argument as a String array.

"envp - array of strings, each element of which has environment variable settings in the format name=value, or null if the subprocess should inherit the environment of the current process."

http://docs.oracle.com/javase/6/docs/api/java/lang/Runtime.html

The way I can get the environment variable is with getenv() which returns a map.

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/System.html

So, in order for me to use exec, I need to convert the map into the form name=value before I call exec.

Can you please show me how I can make this conversion?
TolgarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CPColinSenior Java ArchitectCommented:
If you don't need to change or add any of the environment variables, you can just pass null for that parameter and it'll work fine.
0
CEHJCommented:
Iterate the entrySet() of the Map or if you're feeling lazy, call toString on the Map and do some string splitting/replacement
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TolgarAuthor Commented:
@CPColin: So, you say, I do not need the env in the second argument.

I tried this and it did not work:
final String[] perlCmd = { "someInternalCommand -u A", null, null };

Open in new window


The command that I run on the command line is something like this:

someInternalCommand -u A

Open in new window


This "someInternalCommand" is in our paths. Because the directory that it locates is in the environment variables.

However, I cannot run the same command from Java line the same way.

I put null as you said but it did not work.

Any ideas?
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

CEHJCommented:
someInternalCommand -u A

Open in new window

As i've mentioned before, that should be three string elements. No nulls required
0
TolgarAuthor Commented:
So, can you please send me the exact line that I should put instead of this one: (someInternalCommand is by default on path)

final String[] perlCmd = { "someInternalCommand ", "-u", "A" };

Open in new window

0
TolgarAuthor Commented:
@CEHJ: Can you please show me how I will do "Iterate the entrySet() of the Map" ?

Thanks,
0
CEHJCommented:
So, can you please send me the exact line that I should put instead of this one:
That IS the exact command, or should be (which is not what you had before)

@CEHJ: Can you please show me how I will do "Iterate the entrySet() of the Map" ?
I don't think you'll need to do that unless you're using a different environment than the one that you're starting (why would you be?)
0
TolgarAuthor Commented:
I did this and it worked;

       
 public static void runcheck() throws IOException {
            
            Map<String, String> env = System.getenv();
            String [] environment = new String[env.size()];
            int i = 0;
            for (Map.Entry<String, String> entry : env.entrySet())
            {
                environment[i] = entry.getKey() + "=" + entry.getValue();
                i++;
            }
           
            String OS = System.getProperty("os.name").toLowerCase();
            final String perlCmd;
            
            if (OS.indexOf("win") >= 0){
                perlCmd = "cmd.exe /C \"xxx.bat -u A\"";
            }
            else{
                perlCmd = "xxx -u A";
            }
            
            Runtime check_runtime = Runtime.getRuntime();
            Process check_process = null;

            try {
                check_process = check_runtime.exec(perlCmd, environment);
                check_process.waitFor();
            } catch (Exception e) {
                System.out.println("error executing " + perlCmd);
            }
            

            /* dump output stream */
            InputStream is = check_process.getInputStream();
            BufferedReader reader = new BufferedReader
                ( new InputStreamReader(is));
            String sLine;
            while ((sLine = reader.readLine()) != null) {
                System.out.println(sLine);
            }
            System.out.flush();

            /* print final result of process */
            System.err.println("Exit status=" + check_process.exitValue());
            return;

Open in new window

0
CEHJCommented:
check_process = check_runtime.exec(perlCmd);

Open in new window


Try changing to the above and see what happens
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.