My apology if this has been discussed before, but I am having a hard time finding info on this. If I have a binary that is requiring me to have an environment set up (e.g., CLASSPATH, PATH, and application related environment variables), how do I provide this to the binary without doing . ./.profile; . appenv.sh; mybinary with getRuntime()? This works, by the way if I use /bin/ksh as the command and my command as a parameter to it, but I do not want to have to provide a shell like this because it interfers with a requirement of the program. I tried collecting all env output into a string array called envp and passing it to getRuntime(command, envp), but the program still complains it cannot find its libraries, which is included in CLASSPATH that is in envp for example), so it does not appear like the environment is getting to the binary. Please note that there is over 80 or so environment variables that need to get passwd in, so this is not a request on how to pass a few environment to the binary. It is a request to understand why getRuntime(command, envp) did not do what I thought it was going to do. For overkill I also assigned all envp key/values to a Properties variable (not sure how the binary could take advantage of this).
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
posted 11 years ago
Thanks for replying. I am a bit confused though (obviously -- sigh), but I thought that using the overloading process is what I was doing when I did getRuntime(command, envp), where command was a string with the command and envp was a string array of environment variables (e.g., envp="CLASSPATH=/location"). Please clarify.
What command are you trying to run. If you want the new process to inherit the same environment as the current process, then you don't need to pass an envp parameter to Runtime.exec. As it says in the Javadoc for Runtime.exec(String command) and Runtime.exec(String command)
This has exactly the same effect as exec(cmdarray, null).
And if you look at the Javadoc for Runtime.exec(String command, String envp) and Runtime.exec(String command, String envp)
If envp is null, the subprocess inherits the environment settings of the current process.
So, if your new process is not picking up an environment variable properly, it means the variable is not set in the current process or your new process is doing something wrong.
posted 11 years ago
I don't mean to be dense, so bare with me. I did not want users to pre-source the environment (i.e., run a script and .profile to source their environment before running my program). Consequently, when my program runs, it does not have an environment set that is required for the program to work. I was trying to fill envp with the key=value environment and pass it into getRuntime(). I was able to run env, extract out the stuff I don't want and keep what I want and put that in the envp string array. I then passed into getRuntime() the command and envp. This is not working for some reason. Is the format correct that I am putting in envp? In that, I was putting into envp for example envp="KEY=value"? Do you have an example of passing into getRuntime() an environment? For some reason finding that example is hard on the web.
Switching from electric heat to a rocket mass heater reduces your carbon footprint as much as parking 7 cars. Tiny ad:
how do I do my own kindle-like thing - without amazon