Link to home
Start Free TrialLog in
Avatar of Tolgar
Tolgar

asked on

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?
Avatar of CPColin
CPColin
Flag of United States of America image

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.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tolgar
Tolgar

ASKER

@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?
someInternalCommand -u A

Open in new window

As i've mentioned before, that should be three string elements. No nulls required
Avatar of Tolgar

ASKER

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

Avatar of Tolgar

ASKER

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

Thanks,
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?)
Avatar of Tolgar

ASKER

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

check_process = check_runtime.exec(perlCmd);

Open in new window


Try changing to the above and see what happens