Java Snippet: Display Your Operating System Name

1
28

Description

Learn Java with simple programming examples.

This snippet describes how to look up and display the name of the operating system running on your computer.

Java provides the System.getProperty() method which returns one of several interesting bits of information about your computer environment.

One of the bits of information is the name of the operating system that the program is running on. Simple pass a string constant “os.name” to the method, and it will return a result of type String.

The following program does this, and has an extra instruction to display the operating system name to the user.

Source Code

  1. Type (or copy) the program shown below into your computer, compile it, and then run it.
  2. Examine the output. My system OS name is “Windows 7”.
  3. Try running the program on different computers. What results do you get?
  4. Extend the program to do something useful depending upon whether the user running your program is on a Linux system or a Mac OS X system.

Compile and Run Screenshot

Compile and execution of DisplayOSname.java which displays the name of the operating system running on your computer.

 

1 COMMENT

  1. […] methods can return Strings and other data types. An interesting application of this is Java Snippet: Display Your Operating System Name which calls the System.getProperty() method to return the name of the operating system your […]

Leave a Reply