Click to See Complete Forum and Search --> : Accessing WAS6.0 Environment variables...
javasweeper
10-27-2006, 12:09 PM
Hi,
I have created an environment variable and am trying to access it thru my code.
But I am always getting Namenotfound exception.
Here is the code I am using:
javax.naming.Context initialContext = new
javax.naming.InitialContext();
javax.naming.Context env = (javax.naming.Context)
initialContext.lookup("java:comp/env");
Set envSet = env.getEnvironment().keySet();
String APP_INSTALL_ROOT = (String)
env.lookup("MY_INSTALL_PATH");
Is there anything wrong with the code or is there any other way to access this variable? I know that I can access the server variables using System.getProperties() but that will not contain my variable value.
Thank you in advance.
chazzy
10-27-2006, 12:47 PM
how avoid system.getenv ?
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#getenv(java.lang.String)
javasweeper
10-27-2006, 02:21 PM
Nope. I want to access my environment variable using java code. I want to read it and do some business functionality based on that value.
chazzy
10-27-2006, 06:25 PM
ok...
like I said. use System.getenv(String variableName);
look at the link I sent you...
And most of the quote from javadoc for java.lang.System getenv(java.lang.String name)
getenv
public static String getenv(String name)
Gets the value of the specified environment variable. An environment variable is a system-dependent external named value.
If a security manager exists, its checkPermission method is called with a RuntimePermission("getenv."+name) permission. This may result in a SecurityException being thrown. If no exception is thrown the value of the variable name is returned.
System properties and environment variables are both conceptually mappings between names and values. Both mechanisms can be used to pass user-defined information to a Java process. Environment variables have a more global effect, because they are visible to all descendants of the process which defines them, not just the immediate Java subprocess. They can have subtly different semantics, such as case insensitivity, on different operating systems. For these reasons, environment variables are more likely to have unintended side effects. It is best to use system properties where possible. Environment variables should be used when a global effect is desired, or when an external system interface requires an environment variable (such as PATH).
On UNIX systems the alphabetic case of name is typically significant, while on Microsoft Windows systems it is typically not. For example, the expression System.getenv("FOO").equals(System.getenv("foo")) is likely to be true on Microsoft Windows.
Parameters:
name - the name of the environment variable
Returns:
the string value of the variable, or null if the variable is not defined in the system environment