How to get env variables

Hi,

I've a .cmd file that sets some env variable.

My java application would run this scipt file. I need to know the value of the env variable set in my java application,. Can anyone help me out here?

For ex,

Script.cmd

set sayhello="hai"

MyJava.java

This will run Script.cmd using Runtime object. How do i get the value of sayhello in MyJava.java

I don't want to echo the value of sayhello in Script.cmd and then use BufferedReader to read the value. System.getEnv() dosen't work here as this is being set within a script file.. Any better solution?

[647 byte] By [Harish_MCAa] at [2007-11-27 8:53:01]
# 1
Check out http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#getenv().
ProggerFromKupfera at 2007-7-12 21:09:40 > top of Java-index,Java Essentials,New To Java...
# 2
No where it proved to be the solution. Thanks for that effort. Guys frame a better solution
Harish_MCAa at 2007-7-12 21:09:41 > top of Java-index,Java Essentials,New To Java...
# 3
Why do you want to use system environment variables? Wouldn't a platform indepentent solution be better? Store that value in a preferences file and you can very easily access it without having to make any calls to native resources.
hunter9000a at 2007-7-12 21:09:41 > top of Java-index,Java Essentials,New To Java...
# 4
> System.getEnv() dosen't work here as this is being set within a script file.Huh? You can revert to something like this in the starting script:java -Dsayhello="$sayhello" ..
BIJ001a at 2007-7-12 21:09:41 > top of Java-index,Java Essentials,New To Java...
# 5

Guys I work for a product based software company. Before going for the build it is required to execute setenv.script file. But this file sets a env variable valled BUILD_HOME. I cannot make any change to the script file.

I have a webapp. This webapp runs again a script file called mydev.cmd. This script file inturn calls setenv.cmd file and does the build. After completing the execution at some point of my webapp i need the value of BUILD_HOME.. This is the exact problem statement. Would greatly appreciate a better solution.

Harish_MCAa at 2007-7-12 21:09:41 > top of Java-index,Java Essentials,New To Java...
# 6

> I have a webapp. This webapp runs again a script file

> called mydev.cmd. This script file inturn calls

> setenv.cmd file and does the build. After completing

> the execution at some point of my webapp i need the

> value of BUILD_HOME.. This is the exact problem

> statement. Would greatly appreciate a better solution.

First, you need to understand how environment variables work. They are passed from parent program to child, not from child to parent. See this for more info: http://en.wikipedia.org/wiki/Environment_variable

So, the solution -- the only solution -- is that your script "mydev.cmd" write the value of that variable to some location, then your web-app retrieves that location. The best choice is probably a temporary file, using a name passed to "mydev.cmd" when it's invoked.

kdgregorya at 2007-7-12 21:09:41 > top of Java-index,Java Essentials,New To Java...
# 7

Writing the value of BUILD_DIR from mydev.cmd to a file(or as u said to any location) wud be at the same cost of echoing the value of BUILD_DIR from mydev.cmd and opening a reader to get the value of BUILD_DIR. Sorry frens, seems like this doesn't frame a quality solution. Lemme hard code the value of BUILD_DIR in my webapp.

Thanks for all the help.

Harish_MCAa at 2007-7-12 21:09:41 > top of Java-index,Java Essentials,New To Java...
# 8
> Sorry frens, seems like this doesn't frame a quality> solution. Lemme hard code the value of BUILD_DIR in> my webapp.Oh yeah, hardcoding a value is such a quality solution.Make sure you hide it some place that no-one will find.
Captain.Obviousa at 2007-7-12 21:09:41 > top of Java-index,Java Essentials,New To Java...
# 9

I know that it isn't a good practice. But the problem crops coz i can't experiment much with the script file.My sole idea is to make the script file look pretty simple. setenv.cmd isn't so simple. It sets lotsa env variables.Probably more than 200. And for each request my script invokes this script file. I can't open a reader to parse the script values. This causes severe overhared at the server. And abt script executing another script, well this is the suggested way of goin for product buid and i can't revert this. Problem is only coz of my inability to get the env value set in another script file. There'z no way left besides running the setenv.cmd file from mydev.cmd. It wud be helpful if u provide me a solution withought reverting back all these approaches. So plz rather than teling me to avoid mydev.cmd execution, give me a better answer. I do appreciate ur answers so far u gave. But those were all i knew. If i wanted to do those i wouldn't have posted this topic here. This is a challenging task unless we don't believe in followin dirty tricks. Thanks to one 'n all.............................

Harish_MCAa at 2007-7-12 21:09:41 > top of Java-index,Java Essentials,New To Java...