System class (II)
27 December 2007The attributes of current working environment are defined through properties, which are in key/value pair. The System class provides a method getProperty() which can be used to get the current working environment useful attributes. In this post, I will present examples to show how to get the working environment properties.
System properties are initialized when runtime starts. Review the following example:
System.out.println("User directory: " + System.getProperty("user.dir")); System.out.println("File separator: " + System.getProperty("file.separator")); System.out.println("Class path: " + System.getProperty("java.class.path")); System.out.println("Class version: " + System.getProperty("java.class.version")); System.out.println("Java home: " + System.getProperty("java.home")); System.out.println("Java vendor: " + System.getProperty("java.vendor")); System.out.println("Java vendor url: " + System.getProperty("java.vendor.url")); System.out.println("Java version: " + System.getProperty("java.version")); System.out.println("Line separator: " + System.getProperty("line.separator")); System.out.println("OS arch: " + System.getProperty("os.arch")); System.out.println("OS name: " + System.getProperty("os.name")); System.out.println("Path separator: " + System.getProperty("path.separator")); System.out.println("User dir: " + System.getProperty("user.dir")); System.out.println("User home: " + System.getProperty("user.home")); System.out.println("User name: " + System.getProperty("user.name"));
Output:
ser directory: C:\Documents and Settings\javauser\workspace\Networking File separator: \ Class path: C:\Documents and Settings\ javauser \workspace\Networking\bin Class version: 49.0 Java home: C:\Program Files\Java\jre1.5.0_11 Java vendor: Sun Microsystems Inc. Java vendor url: http://java.sun.com/ Java version: 1.5.0_11 Line separator: OS arch: x86 OS name: Windows XP Path separator: ; User dir: C:\Documents and Settings\javauser \workspace\Networking User home: C:\Documents and Settings\javauser User name: javauser
I displayed all the properties on the console using getProperty(…) method of the System class. There properties are very useful in certain scenarios for example, if you want your application to behave differently on different operation systems or on different Java versions, then the os.name and java.version properties are of interest.
Happy coding.
Related Posts:
Top Of Page | Trackback
If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.
It will look like this: System class (II)