how to detect OS in java

Discussion in 'Java' started by S!mb@, Jul 14, 2004.

  1. S!mb@

    S!mb@ Guest

    Hi,

    I'm looking for a way to detect on which OS my program is running.
    Is there a variable or someting ?

    thanks in advance,

    jerem.
     
    S!mb@, Jul 14, 2004
    #1
    1. Advertising

  2. S!mb@ wrote:

    > Hi,
    >
    > I'm looking for a way to detect on which OS my program is running.
    > Is there a variable or someting ?
    >
    > thanks in advance,
    >
    > jerem.


    Hi Jerem,

    String os = System.getProperty("os.name");

    See also the complete list of system properties at
    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#getProperties()

    --
    Thomas<dot>Fritsch<squiggle>ops<dot>de
     
    Thomas Fritsch, Jul 14, 2004
    #2
    1. Advertising

  3. S!mb@

    S!mb@ Guest

    S!mb@, Jul 14, 2004
    #3
  4. S!mb@

    Alan Meyer Guest

    "S!mb@" <S!mb@nop> wrote in message
    news:40f52b6e$0$29377$...
    > Hi,
    >
    > I'm looking for a way to detect on which OS my program is running.
    > Is there a variable or someting ?
    >
    > thanks in advance,
    >
    > jerem.


    As a followup to Thomas' correct answer, here's a little
    program to show all properties. I haven't done any
    real exception handling. If you get the message
    "Completed=false", you'll have to check for actual
    exceptions like SecurityException.

    Alan

    import java.util.*;

    class GetProp {

    public static void main (String[] args) {

    boolean completed = false;

    try {
    Properties py = System.getProperties();
    Enumeration e = py.propertyNames();
    while (e.hasMoreElements()) {
    String key = (String) e.nextElement();
    String value = System.getProperty (key);
    System.out.println (key + " = " + value);
    }
    completed = true;
    }
    finally {
    System.out.println ("Completed=" + completed);
    }
    }
    }
     
    Alan Meyer, Jul 14, 2004
    #4
  5. Hi Alan,

    Alan Meyer wrote:

    >"S!mb@" <S!mb@nop> wrote in message
    >
    >
    >>I'm looking for a way to detect on which OS my program is running.
    >>Is there a variable or someting ?
    >>
    >>

    >...
    >
    >import java.util.*;
    >
    >class GetProp {
    >
    > public static void main (String[] args) {
    >
    > boolean completed = false;
    >
    > try {
    > Properties py = System.getProperties();
    > Enumeration e = py.propertyNames();
    > while (e.hasMoreElements()) {
    > String key = (String) e.nextElement();
    > String value = System.getProperty (key);
    > System.out.println (key + " = " + value);
    > }
    >
    >

    // BTW: you can replace the 7 lines above by:
    System.getProperties().store(System.out, null);

    > completed = true;
    > }
    > finally {
    > System.out.println ("Completed=" + completed);
    > }
    > }
    >}
    >
    >
    >

    --
    Thomas<dot>Fritsch<squiggle>ops<dot>de
     
    Thomas Fritsch, Jul 14, 2004
    #5
  6. In article <>,
    Thomas Fritsch <> wrote:

    > Hi Alan,
    >
    > Alan Meyer wrote:
    >
    > >"S!mb@" <S!mb@nop> wrote in message
    > >
    > >>I'm looking for a way to detect on which OS my program is running.
    > >>Is there a variable or someting ?
    > >>

    > >...
    > >
    > >import java.util.*;
    > >
    > >class GetProp {
    > >
    > > public static void main (String[] args) {
    > >
    > > boolean completed = false;
    > >
    > > try {
    > > Properties py = System.getProperties();
    > > Enumeration e = py.propertyNames();
    > > while (e.hasMoreElements()) {
    > > String key = (String) e.nextElement();
    > > String value = System.getProperty (key);
    > > System.out.println (key + " = " + value);
    > > }
    > >

    > // BTW: you can replace the 7 lines above by:
    > System.getProperties().store(System.out, null);
    >
    > > completed = true;
    > > }
    > > finally {
    > > System.out.println ("Completed=" + completed);
    > > }
    > > }
    > >}


    Using the store() method is canonical and more economical, but a
    security manager may preclude using getProperties(). In
    particular, "...if the security manager does not permit the
    getProperties operation, it may choose to permit the
    getProperty(String) operation."

    John
    ----
    jmatthews at wright dot edu
    www dot wright dot edu/~john.matthews/
     
    John B. Matthews, Jul 14, 2004
    #6
  7. On Wed, 14 Jul 2004 19:12:48 GMT, John B. Matthews wrote:

    > In
    > particular, "...if the security manager does not permit the
    > getProperties operation, it may choose to permit the
    > getProperty(String) operation."


    'Unsigned Applets' is one distinct case in point.

    I had to call the properties by name in my unsigned,
    java 1.1 compatible properties applet here..
    <http://www.physci.org/pc/property.jsp>

    In contrast, the Java Glossay's 'WassUp' applet is
    signed, and can delve deeply into the system guts..
    <http://mindprod.com/wassup.html>

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
     
    Andrew Thompson, Jul 14, 2004
    #7
    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. Peter Blatt
    Replies:
    0
    Views:
    379
    Peter Blatt
    Oct 9, 2004
  2. Ryan Stewart

    Detect Java enabled browser

    Ryan Stewart, Dec 24, 2004, in forum: Java
    Replies:
    4
    Views:
    4,485
    Andrew Thompson
    Dec 25, 2004
  3. Replies:
    0
    Views:
    651
  4. Frank Wedekind
    Replies:
    2
    Views:
    3,608
    cstamas82
    Apr 28, 2007
  5. jojo
    Replies:
    29
    Views:
    4,418
    Larry Barowski
    Sep 6, 2005
Loading...

Share This Page