Title: Untitled Document
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

System Properties

Most things in java just automatically work the same on all platforms. Sometimes it is possible for your code to sneak in little characteristics which assume you are running on one platform (i.e. Unix or DOS). The System class has a few methods which can be used to find out what your platform characteristics are. This program explores this a little.


import java.util.*;
class SystemProps
{
  public static void main(String[] args)
  {
    String userDir, userHome, userName;
    String lineSeparator, fileSeparator;

    // Dump out all of the properties in System
    Properties props;
    props = System.getProperties();
    props.list(System.out);
    System.out.println("***************************************");

    // Access specific Properties

    System.out.println("os.name:" +
        System.getProperty("os.name"));

    // path.separator shows up in the PATH or CLASSPATH environment
    // variables.
    System.out.println("path.separator: " +
        System.getProperty("path.separator"));

    // file.separator shows up in file names: i.e.:
    // Unix uses / as in /home/chasselb/work
    // Windows uses \ as in C:\work
    fileSeparator = System.getProperty("file.separator");
    System.out.println("file.separator: " + fileSeparator);

    // Unix uses New lines only (10)
    // Windows uses Carriage Return followed by Newline 13,10
    lineSeparator = System.getProperty("line.separator");
    for (int i=0; i < lineSeparator.length(); i++)
        System.out.println("int(line.separator): " +
                       (int)(lineSeparator.charAt(i)));

    // Useful user characteristics.
    userName = System.getProperty("user.name");
    userHome = System.getProperty("user.home");
    userDir = System.getProperty("user.dir");


    System.out.println("usr.name=" + userName);
    System.out.println("usr.home=" + userHome);
    System.out.println("usr.dir=" + userDir);

    // BAD --- Non 100% Java approach for changing directories.
    // Note that I am assuming a windows environment.  The following
    // code is bad on Unix.  Note the stupid choice that
    // Windows made, forces us to use 2 \\ for a real \ because
    // withing strings \ is an escape character.
    System.setProperty("user.dir", "C:\\work2");
    System.out.println("***Platform---- Changed dir: " +
            System.getProperty("user.dir"));

    // Note that just because you can call setProperty, it may not actually
    // change anything in your development environment.  For example,
    // just changing user.dir does not change my current working directory.
    // We will play with this later.

    // BAD -- Non 100% Java approach for changing directories.
    // Note that I am assuming a Unix environment --- this would be
    // a bad on  Windows.
    System.setProperty("user.dir", "/home/cis269/c26900/stuffing");
    System.out.println("***Platform---- Changed dir: " +
            System.getProperty("user.dir"));

    // Good -- 100% Pure Java approach that will work on all platforms
    // !!!!!!!
    userDir = userHome + fileSeparator + "work";
    System.out.println("100% Java filepath: " + userDir);
    System.setProperty("user.dir", userDir);

    // Not so sure I like the fact that it is so easy to change my
    // username identity.  Not sure what ramifications this has
    // for the rest of your Java environment.
    System.setProperty("user.name", "bclinton");
    System.out.println("---- Changed name: " +
            System.getProperty("user.name"));

   }
}


Last Updated: September 2, 2002 4:35 PM