Showing posts with label System.getProperty(). Show all posts
Showing posts with label System.getProperty(). Show all posts

Wednesday, 10 September 2014

Retrive System properties Java

Accessing system properties can be an important part of the code/ J2EE application . Some of the system properties and their corresponding description are given below .

Java system propertyMeaning
file.encodingThe encoding used in text files on the system.
file.separatorThe file separator character. This is usually \ for windows and / for UNIX like systems
java.io.tmpdirThe directory in which temporary files can be created on the local system. System environment variable %TEMP%
line.separatorThe character or sequence of characters that generally mark a "new line" in a text file on the local operating system. On UNIX-like systems, this is typically a single ASCII character 10, whereas on Windows systems, this is typically a two-character sequence (ASCII 10 and ASCII 13).
os.nameThe name of the local operating system, such as "Linux" or "Windows 7".
os.versionA version number of the local operating system, such as "6.0" or "2.6.18-stab02".
path.separatorThe separator generally used to separate lists of paths on that operating system, for example a colon (":"). Note that this is not the directory separator used inside paths.
user.countryThe ISO code of the operating system's (or local user's) configured country.
user.dirThe local directory from which the Java process has been started, and from which files will be read/written by default unless a path is specified.
user.homeThe current user's "home" directory, such as C:\Users\Fred on Windows systems, or /home/sandesh/ on UNIX-like systems.
user.languageThe ISO code of the operating system's (or local user's) configured language, such as "en" for English.
user.nameThe local user's system user name. On Windows systems, this is typically close to a "real life" name. On UNIX-like systems, it is common for user names to be all lower case letters.

 import java.util.Map;  
 public class ReadSystemProperties {  
      public static void main(String[] args) {  
           Map envVars = System.getenv(); // Gives the environment variable values.  
           System.out.println(envVars);  
           String osVersion = System.getProperty("os.version"); // Retrives the version of os you are working on.  
           System.out.println("OS Version - \t" + osVersion);  
           String osName = System.getProperty("os.name"); // Retrives the os you are working on.  
           System.out.println("OS Version - \t" + osName);  
           String fileEncoding = System.getProperty("file.encoding"); // Retrives the os you are working on.  
           System.out.println("File Encoding - \t" + fileEncoding);  
           String fileSeparator = System.getProperty("file.separator"); // Retrives the os you are working on.  
           System.out.println("File Separator - \t" + fileSeparator);  
      }  
 }  

Wednesday, 3 September 2014

How to find which OS you are working on ?

       For some projects you will need to find out which OS you are working on or on which Operating System the code is deployed. This may be needed in certain cases like - File System Handling ,
Data upload system, Electronic Records System etc because the file organization of different operating systems are indifferent.


For the list of other system properties/ System environment variables please click here


       The following code sample in Java gives this -



 package com.util;  
 public class FindOS{  
      private static String OS = System.getProperty("os.name").toLowerCase();  
      public static void main(String[] args) {  
           System.out.println(OS);  
           if (isWindows()) {  
                System.out.println("This is Windows");  
           } else if (isMac()) {  
                System.out.println("This is Mac");  
           } else if (isUnix()) {  
                System.out.println("This is Unix or Linux");  
           } else if (isSolaris()) {  
                System.out.println("This is Solaris");  
           } else {  
                System.out.println("Your OS is not support!!");  
           }  
      }  
      public static boolean isWindows() {  
           return (OS.indexOf("win") >= 0);  
      }  
      public static boolean isMac() {  
           return (OS.indexOf("mac") >= 0);  
      }  
      public static boolean isUnix() {  
           return (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0 );  
      }  
      public static boolean isSolaris() {  
           return (OS.indexOf("sunos") >= 0);  
      }  
 }