Re: to get environment variables...

Discussion in 'Java' started by shakah, Jul 10, 2008.

  1. shakah

    shakah Guest

    On Jul 10, 8:49 am, wrote:
    > How can I get environment variables not using method System.getenv()?


    You could iterate over the Properties object returned by
    System.getProperties(), or run an external Process and read/parse the
    output, e.g.:

    public class testprops {
    public static void main(String [] args)
    throws Exception {
    // ...JRE props
    java.util.Properties props = System.getProperties() ;
    for(java.util.Enumeration ep=props.propertyNames();
    ep.hasMoreElements(); ) {
    String k = (String) ep.nextElement() ;
    System.out.println(k + ": '" + System.getProperty(k) + "'") ;
    }

    // ...OS-level environment vars
    java.lang.Process p =
    java.lang.Runtime.getRuntime().exec("printenv") ;
    p.waitFor() ;

    java.io.InputStream is = null ;
    try {
    is = p.getInputStream() ;
    java.io.BufferedInputStream bis = new
    java.io.BufferedInputStream(is) ;

    byte [] ab = new byte[64000] ;
    int nTotalBytesRead = 0 ;
    while(nTotalBytesRead < 64000) {
    int nBytesRead = bis.read(ab, nTotalBytesRead, 64000 -
    nTotalBytesRead) ;
    if(nBytesRead < 0) {
    break ;
    }
    else {
    nTotalBytesRead += nBytesRead ;
    }
    }

    System.out.println(new String(ab, 0, nTotalBytesRead)) ;
    }
    }
    }
     
    shakah, Jul 10, 2008
    #1
    1. Advertising

  2. shakah

    Blueparty Guest

    I never understood why is so handy thing as getenv() deprecated.

    B
     
    Blueparty, Jul 10, 2008
    #2
    1. Advertising

  3. shakah

    shakah Guest

    On Jul 10, 1:52 pm, Eric Sosman <> wrote:
    > Blueparty wrote:
    > > I never understood why is so handy thing as getenv() deprecated.

    >
    >      As far as I can tell, it isn't.  The Javadoc mentions some
    > portability gotchas, but doesn't list System.getenv() -- either
    > of them -- as deprecated.
    >
    > --
    >


    Maybe it depends on the Java version?

    The following (for 1.4.2) has it marked as deprecated:
    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html
     
    shakah, Jul 10, 2008
    #3
  4. Blueparty wrote:
    > I never understood why is so handy thing as getenv() deprecated.


    Originally, because it was too tied to the design of Unix.

    But, what with nearly all full-function operating systems nowadays being
    built on Unix (MacOS X), or cloned from Unix (Linux), or with alternate
    Unix sides (z/OS) or vaguely imitating Unix (Windows), they decided to
    un-deprecate and reactivate it as of Java 5.



    --
    John W. Kennedy
    "...if you had to fall in love with someone who was evil, I can see
    why it was her."
    -- "Alias"
     
    John W Kennedy, Jul 10, 2008
    #4
  5. shakah

    Arne Vajhøj Guest

    Eric Sosman wrote:
    > shakah wrote:
    >> On Jul 10, 1:52 pm, Eric Sosman <> wrote:
    >>> Blueparty wrote:
    >>>> I never understood why is so handy thing as getenv() deprecated.
    >>> As far as I can tell, it isn't. The Javadoc mentions some
    >>> portability gotchas, but doesn't list System.getenv() -- either
    >>> of them -- as deprecated.

    >>
    >> Maybe it depends on the Java version?
    >>
    >> The following (for 1.4.2) has it marked as deprecated:
    >> http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html

    >
    > Deprecation usually depends on the version; very few things
    > are deprecated by their inventors when first released! But this
    > seems to be an unusual case: getenv(String) is deprecated in 1.4.2
    > (which presumably means it was non-deprecated earlier), but as of
    > 1.6 it's non-deprecated again and another getenv() method has
    > been added!
    >
    > Maybe Java needs a @rehabilitated tag ...


    It was "rehabilitated" in 1.5.

    http://java.sun.com/j2se/1.5.0/docs/relnotes/features.html#envars

    My guess would be due to popular demand !

    Arne
     
    Arne Vajhøj, Jul 10, 2008
    #5
  6. shakah

    EJP Guest

    shakah wrote:
    > You could iterate over the Properties object returned by
    > System.getProperties(), or run an external Process and read/parse the
    > output, e.g.:


    That would give you the system properties, not the environment variables!
     
    EJP, Jul 11, 2008
    #6
  7. shakah

    shakah Guest

    On Jul 11, 2:50 am, Lew <> wrote:
    > shakah wrote:
    > >>> The following (for 1.4.2) has it marked as deprecated:
    > >>> http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html

    >
    > Given that version 1.4 is dead, gone, retired, kaput, over and finished, it's
    > not really any indication of anything to go by its documentation unless you
    > are maintaining a legacy 1.4 system that refuses to acknowledge its undead status.


    Production systems can be stubborn zombies sometimes...
     
    shakah, Jul 11, 2008
    #7
  8. shakah

    Tom Anderson Guest

    On Thu, 10 Jul 2008, Eric Sosman wrote:

    > shakah wrote:
    >> On Jul 10, 1:52 pm, Eric Sosman <> wrote:
    >>> Blueparty wrote:
    >>>> I never understood why is so handy thing as getenv() deprecated.
    >>> As far as I can tell, it isn't. The Javadoc mentions some
    >>> portability gotchas, but doesn't list System.getenv() -- either
    >>> of them -- as deprecated.
    >>>
    >>> --
    >>>

    >>
    >> Maybe it depends on the Java version?
    >>
    >> The following (for 1.4.2) has it marked as deprecated:
    >> http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html

    >
    > Deprecation usually depends on the version; very few things
    > are deprecated by their inventors when first released! But this
    > seems to be an unusual case: getenv(String) is deprecated in 1.4.2
    > (which presumably means it was non-deprecated earlier), but as of
    > 1.6 it's non-deprecated again and another getenv() method has
    > been added!
    >
    > Maybe Java needs a @rehabilitated tag ...


    Hee! Brilliant! Or maybe we should be able to deprecate the deprecation.
    At the moment, it's a bit 1984 - "getenv is not deprecated; getenv has
    never been deprecated" ...

    tom

    --
    20 Minutes into the Future
     
    Tom Anderson, Jul 11, 2008
    #8
  9. shakah

    Roedy Green Guest

    On Fri, 11 Jul 2008 02:50:12 -0400, Lew <> wrote,
    quoted or indirectly quoted someone who said :

    >
    >Given that version 1.4 is dead, gone, retired, kaput, over and finished, it's
    >not really any indication of anything to go by its documentation unless you
    >are maintaining a legacy 1.4 system that refuses to acknowledge its undead status.


    Deprecated mainly means a method MAY disappear in future, so is not
    recommended.

    Sometimes it means there are problems with the current implementation,
    but it still should work, sort of.

    I think the problem with getEnv was that the Mac did not have an
    environment. Either Sun had to fake one or not support getEnv on the
    mac. i.e. ~/.MacOSX/environment.plist

    see http://mindprod.com/jgloss/environment.html
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Jul 11, 2008
    #9
  10. Lew wrote:
    > shakah wrote:
    >> On Jul 11, 2:50 am, Lew <> wrote:
    >>> shakah wrote:
    >>>>>> The following (for 1.4.2) has it marked as deprecated:
    >>>>>> http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html
    >>> Given that version 1.4 is dead, gone, retired, kaput, over and
    >>> finished, it's
    >>> not really any indication of anything to go by its documentation
    >>> unless you
    >>> are maintaining a legacy 1.4 system that refuses to acknowledge its
    >>> undead status.

    >>
    >> Production systems can be stubborn zombies sometimes...

    >
    > I'm working a major contract that is only just now transitioning off
    > Java 1.3.


    I am pretty sure that there are more Java EE production sites running on
    1.3.1 and 1.4.2 combined than on 1.6 - a lot more.

    Arne
     
    Arne Vajhøj, Jul 12, 2008
    #10
  11. shakah

    Daniel Pitts Guest

    Blueparty wrote:
    > I never understood why is so handy thing as getenv() deprecated.
    >
    > B

    It isn't deprecated as far as I can tell.

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Jul 12, 2008
    #11
  12. shakah

    Arne Vajhøj Guest

    Daniel Pitts wrote:
    > Blueparty wrote:
    >> I never understood why is so handy thing as getenv() deprecated.

    > It isn't deprecated as far as I can tell.


    It was 1.1.x - 1.4.x !

    Arne
     
    Arne Vajhøj, Jul 12, 2008
    #12
  13. Lew wrote:
    > Blueparty wrote:
    >>> I never understood why is so handy thing as getenv() deprecated.

    >
    > Daniel Pitts wrote:
    >> It isn't deprecated as far as I can tell.

    >
    > Upthread they discuss the re-undeprecation of that method. The OP was
    > looking at Java 1.4 docs, and relying on information about an obsolete
    > product. The 'getenv()' methods are not now deprecated.


    Somehow SUN made a mess out of this one.

    Arne
     
    Arne Vajhøj, Jul 13, 2008
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RC
    Replies:
    4
    Views:
    8,019
    Tor Iver Wilhelmsen
    Jul 12, 2005
  2. Replies:
    5
    Views:
    659
  3. Benoit Lefebvre

    Get the Compile-time Environment Variables ?

    Benoit Lefebvre, Nov 29, 2007, in forum: C Programming
    Replies:
    4
    Views:
    669
    Keith Thompson
    Nov 29, 2007
  4. Replies:
    9
    Views:
    955
  5. Ramon Barquier

    get environment variables

    Ramon Barquier, Nov 6, 2003, in forum: Javascript
    Replies:
    1
    Views:
    131
    RIck Measham
    Nov 6, 2003
Loading...

Share This Page