| The class java.util.Properties 
              is a subclass of Hashtable. 
              Java uses Properties 
              tables to hold environment variables in key/value string 
              pairs. See the API 
              Specification for a listing of the keys for the Properties 
              instance returned from the method System.getProperties() Applets are restricted by the SystemManager 
              from accessing many of the properties such as the user's directory 
              info. This applet shows the values for those properties that are 
              accessible by applets:  
              
                 
                  | SysProperties |   
                  |  import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;
 import java.util.*;
 
 /** List the system properties available to applets and
 * to applications.
 **/
 public class SysProperties extends JApplet
 implements ActionListener
 {
 // A Swing textarea for display of string info
 JTextArea fTextArea = null;
 
 /** Set up the user interface.**/
 public void init () {
 
 JPanel panel = new JPanel (new BorderLayout 
                      ());
 
 // Create an instance of DrawingPanel
 fTextArea = new JTextArea ();
 
 // Create an instance of DrawingPanel
 fTextArea = new JTextArea ();
 fTextArea.setEditable (false);
 
 // Add to a scroll pane so that 
                      a long list of
 // computations can be seen.
 JScrollPane area_scroll_pane = new 
                      JScrollPane (fTextArea);
 
 panel.add (area_scroll_pane,"Center");
 
 JButton go_button = new JButton 
                      ("Go");
 go_button.addActionListener (this);
 
 JButton clear_button = new JButton 
                      ("Clear");
 clear_button.addActionListener (this);
 
 JPanel control_panel = new JPanel 
                      ();
 control_panel.add (go_button);
 control_panel.add (clear_button);
 
 panel.add (control_panel,"South");
 
 // Add text area with scrolling 
                      to the contentPane.
 add (panel);
 
 } // init
 
 /**  Print out the system properties 
                      list. **/
 public void start () {
 
 try {
 Properties sysProps 
                      = System.getProperties ();
 int i=0;
 Enumeration names = 
                      sysProps.propertyNames ();
 while  (names 
                      .hasMoreElements ()) {
 String key 
                      =  (String)names .nextElement ();
 i++;
 print (i 
                      +". " + key + " = " +
 sysProps.getProperty 
                      (key) + "\n");
 }
 }
 catch (Exception e) {
 // If security error 
                      thrown by browser,
 // then only ask for 
                      the following key values.
 
 String [] key =  {
 "java.version",
 "java.vendor",
 "java.class.version",
 "os.name",
 "os.arch",
 "os.version",
 "file.separator",
 "path.separator",
 "line.separator",
 };
 
 for (int i=0; i < key.length; 
                      i++)
 print 
                      (i +". "+ key[i] + " = " +
 System.getProperty 
                      (key[i]) + "\n");
 }
 } // start
 
 /**  Respond to the buttons. **/
 public void actionPerformed (ActionEvent e) 
                      {
 if  (e.getActionCommand 
                      ().equals ("Go"))
 start ();
 else
 fTextArea.setText 
                      (null);
 } // actionPerformed
 
 /**  Send println () output to the 
                      text area. **/
 public void println (String str) {
 fTextArea.append (str + '\n');
 }
 
 /**  Send print () output to the text 
                      area. **/
 public void print (String str) {
 fTextArea.append (str);
 }
 
 /**  Display the applet in a JFrame. 
                      **/
 public static void main (String[] args) {
 int frame_width=350;
 int frame_height=350;
 JApplet applet = new SysProperties 
                      ();
 applet.init ();
 
 // Creat a frame and add the applet 
                      to it.
 JFrame f = new JFrame ("Frame & 
                      Image Demo");
 
 f.getContentPane ().add ( applet);
 f.setSize (new Dimension (frame_width,frame_height));
 f.setVisible (true);
 
 applet.start ();
 } // main
 
 } // class SysProperties
 
 |    A standalone application, on the other hand, has no such restrictions. 
              The above program will run also as an application. Run it on your 
              machine to see your system properties. The output below shows a 
              typical set of properties (line breaks added in some cases to fit 
              within this page frame.)  
              
                 
                  | 1. java.runtime.name 
                    = Java(TM) 2 Runtime Environment, Standard Edition 2. sun.boot.library.path = C:\JAVA\J2SDK1.4.0\JRE\bin
 3. java.vm.version = 1.4.0-b92
 4. java.vm.vendor = Sun Microsystems Inc.
 5. java.vendor.url = http://java.sun.com/
 6. path.separator = ;
 7. java.vm.name = Java HotSpot(TM) Client VM
 8. file.encoding.pkg = sun.io
 9. user.country = US
 10. sun.os.patch.level =
 11. java.vm.specification.name = Java Virtual Machine
 Specification
 12. user.dir = C:\Java\Book\WebCourse\Course\Code\Java\Utilities\Properties
 13. java.runtime.version = 1.4.0-b92
 14. java.awt.graphicsenv = sun.awt.Win32GraphicsEnvironment
 15. java.endorsed.dirs = C:\JAVA\J2SDK1.4.0\JRE\lib\endorsed
 16. os.arch = x86
 17. java.io.tmpdir = C:\WINDOWS\TEMP\
 18. line.separator =
 
 
 19. java.vm.specification.vendor = Sun Microsystems Inc.
 20. user.variant =
 21. os.name = Windows Me
 22. sun.java2d.fontpath =
 23. java.library.path = C:\JAVA\J2SDK1.4.0\BIN;.;
 C:\WINDOWS\SYSTEM;C:\WINDOWS;
 C:\WINDOWS;C:\WINDOWS\COMMAND;
 c:\java\j2sdk1.4.0\bin;
 24. java.specification.name = Java Platform API Specification
 25. java.class.version = 48.0
 26. java.util.prefs.PreferencesFactory =
 java.util.prefs.WindowsPreferencesFactory
 27. os.version = 4.90
 28. user.home = C:\WINDOWS
 29. user.timezone =
 30. java.awt.printerjob = sun.awt.windows.WPrinterJob
 31. file.encoding = Cp1252
 32. java.specification.version = 1.4
 33. user.name = clarklindsey
 34. java.class.path = .
 35. java.vm.specification.version = 1.0
 36. sun.arch.data.model = 32
 37. java.home = C:\JAVA\J2SDK1.4.0\JRE
 38. java.specification.vendor = Sun Microsystems Inc.
 39. user.language = en
 40. awt.toolkit = sun.awt.windows.WToolkit
 41. java.vm.info = mixed mode
 42. java.version = 1.4.0
 43. java.ext.dirs = C:\JAVA\J2SDK1.4.0\JRE\lib\ext
 44. sun.boot.class.path = C:\JAVA\J2SDK1.4.0\JRE\lib\rt.jar;
 C:\JAVA\J2SDK1.4.0\JRE\lib\i18n.jar;
 C:\JAVA\J2SDK1.4.0\JRE\lib\sunrsasign.jar;
 C:\JAVA\J2SDK1.4.0\JRE\lib\jsse.jar;
 C:\JAVA\J2SDK1.4.0\JRE\lib\jce.jar;
 C:\JAVA\J2SDK1.4.0\JRE\lib\charsets.jar;
 C:\JAVA\J2SDK1.4.0\JRE\classes
 45. java.vendor = Sun Microsystems Inc.
 46. file.separator = \
 47. java.vendor.url.bug =
 http://java.sun.com/cgi-bin/bugreport.cgi
 48. sun.cpu.endian = little
 49. sun.io.unicode.encoding = UnicodeLittle
 50. sun.cpu.isalist = pentium i486 i386
 |    Environment Variables From this output, you can see that the list of system properties 
              includes many useful and interesting items. Everything you might 
              need or want to know is not included there, however. One thing that 
              is missing is a list of operating system or command shell environment 
              variables. Alas, unlike C or C++, there is no getEnv() 
              method in Java.  Part of the reason is that Java could conceivably be run on a platform 
              that does not support the concept of environment variables. The 
              expected way to pass environment-variable-like values to a Java 
              application is with the -Dname=value 
              syntax seen a few times in earlier chapters. Using the -D 
              syntax on the java command line effectively adds the specified name 
              and value to the list of system properties. Therefore, if you need 
              to send a system environment variable named SomeEnvVar 
              to your Java code, you can include it on the command line like this:  
               java -Dsome.env.variable=$SomeEnvVar 
                YourClass (Unix/Linux)  or   
              java -Dsome.env.variable=%SomeEnvVar% 
                YourClass (Windows)  Then you access the new system property as follows:   
              String some_value 
                = System.getProperty ("some.env.variable");  Obviously, you can name the system property anything you want. Graphics 
              Settings Information on the platform display is available from the java.awt.Toolkit, 
              which we used in Chapter 
              6: Java to obtain images and in Chapter 
              12: Java to obtain instances of PrintJob. 
              The method   
              int getScreenResolution 
                ()  returns the resolution in dots-per-inch. (Unfortunately, on Windows 
              systems the value returned is actually the font size setting rather 
              than the actual current screen resolution setting.) The method   
              Dimension getScreenSize 
                ()  returns the width and height of the screen in pixels. For example, 
              you can use this to set the location of a frame at the center of 
              the screen with a method like this:   
              public void 
                center () { Dimension screenSize =
 Toolkit.getDefaultToolkit ().getScreenSize 
                ();
 Dimension frameSize = getSize ();
 int x = (screenSize.width - frameSize.width) / 2;
 int y = (screenSize.height - frameSize.height) / 2;
 setLocation (x, y);
 }
 Other graphics information can be obtained via : 
               
                 
                  SystemColor 
                    class: provides via static fields the system windowing colors, 
                    which you could use, for example, for your own interface colors, 
                    as in
 
  
                    myCanvas.setBackground 
                    (SystemColor.window);myCanvas.setForeground (SystemColor.text);
 
 
GraphicsEnvironment.getLocalGraphicsEnvironment() 
                - static method which returns an instance of a concrete 
                subclass of the GraphicsEnvironment 
                abstract class. With a instance of this class you can find out 
                lots of information on the local graphics system. For example, 
                the methods: 
                
               References & Web Resources   Latest update: Dec. 13, 2004 |