0

When using

final Map<String, String> env = new HashMap<String, String>(System.getenv());
env.put("ECLIPSE_PROJ_PATH", fileSelectedPath);
final String[] envVars = SystemUtils.mapToStringArray(env);
try {           

Runtime.getRuntime().exec("/usr/bin/gedit",envVars).waitFor();
} catch (InterruptedException e) {
    e.printStackTrace();
}

it doesn't work an gedit doesn't show up and no exceptions occur.

but when removing the env vars and only use the command in exec method like

Runtime.getRuntime().exec("/usr/bin/gedit").waitFor();

it works correctly.

I need to have my env vars set while running my program.. note : this works fine on windows.

becks
  • 2,656
  • 8
  • 35
  • 64
  • I'd recommend debugging into the code and checking which values are in `env` before you start the execution. Maybe there's something wrong with some value? – blalasaadri Dec 02 '13 at 14:16
  • That's not how you pass Environment variables (e.g. `DISPLAY`). You're passing the environment variables as command line arguments. Also, you should probably use [ProcessBuilder](http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html) instead of `Runtime.exec()`. – Elliott Frisch Dec 02 '13 at 14:16
  • Output the hashmap env - I would be interested to know what it contains. – RenegadeAndy Dec 02 '13 at 14:16
  • what is the point of using waitFor() here ? – Sergei Chicherin Dec 02 '13 at 14:21
  • variables are correct. and process builder gives the same situation. – becks Dec 02 '13 at 15:47
  • the map contains variables in format of "var=val1" – becks Dec 02 '13 at 15:48

1 Answers1

0

I am not sure what SystemUtils.mapToStringArray returns, but it might be easier to use the ProcessBuilder, where environment variables can be provided in a Map.

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

http://docs.oracle.com/javase/6/docs/api/java/lang/ProcessBuilder.html#environment()

Brett Okken
  • 6,210
  • 1
  • 19
  • 25
  • Same situation happens. when I use process builder and add the env vars, it doesn't work. but when I run the command only without adding the variable it works – becks Dec 02 '13 at 15:46
  • Have you attempted to read data from the process output? Is there anything returned from either [output stream](http://docs.oracle.com/javase/6/docs/api/java/lang/Process.html#getOutputStream()) or [error stream](http://docs.oracle.com/javase/6/docs/api/java/lang/Process.html#getErrorStream())? – Brett Okken Dec 06 '13 at 13:54