Java Getting Environment Variables
You want to get the value of “environment variables” from within your Java program.
Solution
Don’t (JDK 1.4 and earlier). Go ahead, but be careful (JDK 1.5).
Explained
The seventh edition of Unix, released in 1979, had an exciting new feature known as environment variables. Environment variables are in all modern Unix systems (including Mac OS X) and in most later command-line systems, such as the DOS subsystem underlying Windows, but are not in some older platforms or other Java runtimes. Environment variables are commonly used for customizing an individual.
computer user’s runtime environment, hence the name. To take one familiar example, on Unix or DOS the environment variable PATH determines where the system looks for executable programs. So of course the issue comes up: “How do I get at environment variables from my Java program?”
The answer is that you can do this in some versions of Java, but you shouldn’t. Java is designed to be a portable runtime environment. As such, you should not depend on operating system features that don’t exist on every single Java platform. I just mentioned several Java platforms that don’t have environment variables.
Let’s try it out using a static method called getenv( ) in class java.lang.System. But remember, you made me do it. First, the code. All we need is the little program shown in Example 2-1.
Example 2-1. GetEnv.java
public class GetEnv {
public static void main(String[] argv) {
System.out.println("System.getenv(\"PATH\") = " + System.getenv("PATH"));
}
}
Let’s try compiling it:
C:\javasrc>javac GetEnv.java
Note: GetEnv.java uses or overrides a deprecated API. Recompile with -deprecation for
details.
That message is seldom welcome news. We’ll do as it says:
C:\javasrc>javac -deprecation GetEnv.java
GetEnv.java:9: Note: The method java.lang.String getenv(java.lang.String) in class
java.lang.System has been deprecated.
System.out.println("System.getenv(\"PATH\") = " + System.getenv("PATH"));
^
Note: GetEnv.java uses or overrides a deprecated API. Please consult the
documentation for a better alternative.
1 warning
But it’s only a warning, right? What the heck. Let’s try running the program!
C:\javasrc>java GetEnv
Exception in thread "main" java.lang.Error: getenv no longer supported, use
properties and -D instead: PATH
at java.lang.System.getenv(System.java:602)
at GetEnv.main(GetEnv.java:9)
Well, of all the non-backward-compatible things! It used to work, in JDK 1.1, but it really and truly doesn’t work anymore in later versions. I guess we’ll just have to do what the error message tells us, which is to learn about “properties and -D instead.” In fact, that’s our very next recipe.
Back to the future: 1.5
In Java 1.5, the getenv( ) method is no longer deprecated, although it still carries the
warning that System Properties (Recipe 2.2) should be used instead. Even among systems
that support them, environment variable names are case-sensitive on some platforms
and case-insensitive on others. However, if you insist, run a program like
GetEnv above, and you’ll get output like the following:
C:\javasrc>java GetEnv
C:\windows\bin;c:\j2sdk1.5\bin;c:\documents and settings\ian\bin
C:\javasrc>
In another addition in 1.5, the no-argument form of the method System.getenv( ) returns all the environment variables, in the form of a non-modifiable StringMap. You can iterate through this list and access all the user’s settings or retrieve multiple environment settings.
Both forms of getenv( ) require you to have permissions to access the environment, so they typically do not work in restricted environments such as applets.
No comments:
Post a Comment