getenv : Java Glossary

go to home page G words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish by Roedy Green ©1996-2009 Canadian Mind Products
index page for letter ⇒ punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
getenv
System.getenv is not considered 100% kosher, but it will get you the environment. It was deprecated in some JDKs, then later resurrected. On windows is it key names are case-insenstive, on other platforms they are case-sensitive. The is no corresponding System. setenv, but you can set up the environment for an execed process. getenv was deprecated for a while then reinstated.
The method is System.getenv( key ) not System.getEnv( key ). The name violates the usual conventions.
Where System.getenv has been deprecated. What do you do instead?
  1. Use a properties file

    This has the advantage of being platform independent.
  2. Use the <APPLET <PARAM tags

    which generate a Property. This only works for Applets.
  3. Pass the parameters in on the command line

    There are several ways of doing this. These techniques only work in applications, not Applets.
    • Named parm technique

      the string "flavour=strawberry" will appear as arg[0] in your main method.
      java.exe MyClass flavour=strawberry
    • Simple parm replacement technique

      the string "strawberry" will appear as arg[0] in your main method. The % works in Windows, NT, 4DOS and 4NT. You may need to do something slightly different in other OSes.
      set flavour=strawberry
      java.exe MyClass %flavour%
    • -D technique

      System property flavour with value "strawberry " will be accessible via System.getProperty("flavour"); Make sure you put the -D before your classname or else you will find "-Dflavour=strawberry " appearing at arg[0] instead. Contrary to rumour, this feature is also available in JDK 1.1. Make sure you use an upper case D.
      set flavour=strawberry
      java.exe -Dflavour=%flavour% MyClass

Learning More

Sun’s Javadoc on System.getenv : available:

CMP homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.58]
You are visitor number 13,183.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/getenv.html J:\mindprod\jgloss\getenv.html