• Post Reply Bookmark Topic Watch Topic
  • New Topic

getRuntime(command, envp) not sourcing environment  RSS feed

 
Mr.David Shapiro
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the overloaded method of Runtime.exec that takes an environment variable.

Follow this link:

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html#exec(java.lang.String[],%20java.lang.String[])
[ June 25, 2007: Message edited by: Joel McNary ]
 
Mr.David Shapiro
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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[0]="CLASSPATH=/location"). Please clarify.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mr.David Shapiro
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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[0]="KEY=value"? Do you have an example of passing into getRuntime() an environment? For some reason finding that example is hard on the web.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!