Java: System Properties

From System Properties you can find information about the operating system, the user, and the version of Java.

The property names (keys) and values are stored in a Properties structure. (See Properties). A Propertiesobject can also be used to store your own program properties in a file.

Getting the System Properties

Typically you get one property at a time by supplying the key in a call to System.getProperty().

  • String System.getProperty(String key) 
    Returns the value of property key as a String.
  • String System.getProperty(String key, String default) 
    Returns the value of property key as a string, or default if the property did not exist.
  • Properties System.getProperties() 
    Returns a Properties object which has the value of all the properties. There are several ways to work with this object; see below for one example.

Example

String userDir = System.getProperty("user.dir");

A list of system properties

Here are the properties that displayed on my system.

awt.toolkit=sun.awt.windows.WToolkit
file.encoding=Cp1252
file.encoding.pkg=sun.io
file.separator=\
java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment
java.awt.printerjob=sun.awt.windows.WPrinterJob
java.class.path=.;c:\classpath\com.fredswartz.utilities.jar;c:\classpath\TableLayout.jar;c:\classpath\swixml.jar;c:\classpath\jdom.jar;c:\classpath\pmd-1.8\lib\pmd-1.8.jar;C:\classpath\pmd-1.8\lib\jaxen-core-1.0-fcs.jar;C:\classpath\com.fredswartz.guiUtils.jar;C:\classpath\com.fredswartz.fmt-0.7.jar;C:\Program Files\IBM\Cloudscape_10.0\lib\derby.jar
java.class.version=49.0
java.endorsed.dirs=C:\Program Files\Java\jdk1.5.0_01\jre\lib\endorsed
java.ext.dirs=C:\Program Files\Java\jdk1.5.0_01\jre\lib\ext
java.home=C:\Program Files\Java\jdk1.5.0_01\jre
java.io.tmpdir=C:\DOCUME~1\Owner\LOCALS~1\Temp\
java.library.path=C:\Program Files\Java\jdk1.5.0_01\bin;.;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;c:\Python22;C:\Program Files\PC-Doctor for Windows\services;c:\ant\bin;c:\Program Files\Java\jdk\bin;c:\Program Files\Java\jdk\jre\javaws;C:\Program Files\Sybase\Adaptive Server Anywhere 6.0\win32;c:\classpath\jcsc/bin;c:\classpath\jcsc\bin;C:\Program Files\Microsoft Visual Studio\Common\Tools\WinNT;C:\Program Files\Microsoft Visual Studio\Common\MSDev98\Bin;C:\Program Files\Microsoft Visual Studio\Common\Tools;C:\Program Files\Microsoft Visual Studio\VC98\bin
java.runtime.name=Java(TM) 2 Runtime Environment, Standard Edition
java.runtime.version=1.5.0_01-b08
java.specification.name=Java Platform API Specification
java.specification.vendor=Sun Microsystems Inc.
java.specification.version=1.5
java.vendor=Sun Microsystems Inc.
java.vendor.url=http://java.sun.com/
java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport.cgi
java.version=1.5.0_01
java.vm.info=mixed mode, sharing
java.vm.name=Java HotSpot(TM) Client VM
java.vm.specification.name=Java Virtual Machine Specification
java.vm.specification.vendor=Sun Microsystems Inc.
java.vm.specification.version=1.0
java.vm.vendor=Sun Microsystems Inc.
java.vm.version=1.5.0_01-b08
line.separator=

os.arch=x86
os.name=Windows XP
os.version=5.1
path.separator=;
sun.arch.data.model=32
sun.boot.class.path=C:\Program Files\Java\jdk1.5.0_01\jre\lib\rt.jar;C:\Program Files\Java\jdk1.5.0_01\jre\lib\i18n.jar;C:\Program Files\Java\jdk1.5.0_01\jre\lib\sunrsasign.jar;C:\Program Files\Java\jdk1.5.0_01\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.5.0_01\jre\lib\jce.jar;C:\Program Files\Java\jdk1.5.0_01\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.5.0_01\jre\classes
sun.boot.library.path=C:\Program Files\Java\jdk1.5.0_01\jre\bin
sun.cpu.endian=little
sun.cpu.isalist=pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86
sun.desktop=windows
sun.io.unicode.encoding=UnicodeLittle
sun.jnu.encoding=Cp1252
sun.management.compiler=HotSpot Client Compiler
sun.os.patch.level=Service Pack 2
user.country=US
user.dir=C:\0www-workingnotes\notes-java-working\io\30properties_and_preferences\40sysprops\SysPropList
user.home=C:\Documents and Settings\Owner
user.language=en
user.name=Owner
user.timezone=
user.variant=
// File: io/properties/SysPropList.java
// Description: Shows system properties.  This must be an application.
//              An applet can't get this information.
// Author: Fred Swartz
// Date:   2 Feb 2005

import java.awt.*;
import javax.swing.*;
import java.util.*;

/** Generic main program. */
public class SysPropList {
    public static void main(String[] args) {
        JFrame window = new JFrame("System Properties");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setContentPane(new SysPropListGUI());
        window.pack();
        window.setVisible(true);
    }
}

/** Panel to display the (limited) GUI intereface. */
class SysPropListGUI extends JPanel {
    JTextArea m_propertiesTA = new JTextArea(20, 40);

    /** Constructor sets layout, adds component(s), sets values*/
    public SysPropListGUI() {
        this.setLayout(new BorderLayout());
        this.add(new JScrollPane(m_propertiesTA), BorderLayout.CENTER);

        //... Add property list data to text area.
        Properties pr = System.getProperties();
        TreeSet propKeys = new TreeSet(pr.keySet());  // TreeSet sorts keys
        for (Iterator it = propKeys.iterator(); it.hasNext(); ) {
            String key = (String)it.next();
            m_propertiesTA.append("" + key + "=" + pr.get(key) + "\n");
        }
    }
}
 

你可能感兴趣的:(java,C++,c,C#,sun)