Java Tutorial/Development/System Properties

Материал из Java эксперт
Версия от 15:30, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Accessing a single System Properties: user.dir

public class MainClass {
  public static void main(String[] a) {
    String currentDir = System.getProperty("user.dir");
    System.out.println(currentDir);
  }
}



C:\Java_Dev\eclipse31\Apache Common


Clear system property: The System.clearProperty() method available in Java 1.5

public class Main {
    public static void main(String[] args) {
        String key = "user.dir";
        System.clearProperty(key);
    }
}





Create a user-defined property or change the value of the current property

public class MainClass {
  public static void main(String[] args) {
    System.setProperty("password", "myPassword");
    System.out.println(System.getProperty("password"));
  }
}



myPassword


Determine operating system using System class

public class Main {
  public static void main(String[] args) {
    System.out.println(System.getProperty("os.name"));
  }
}





Emit a beep by printing the ASCII Bell character to the console

public class Main {
  public static void main(String args[]) {
    // ASCII bell
    System.out.print("\0007");
    System.out.flush();
  }
}





Get Environment Variables

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Main {
  public static void main(String[] args) {
    Map map = System.getenv();
    Set keys = map.keySet();
    Iterator iterator = keys.iterator();
    while (iterator.hasNext()) {
      String key = (String) iterator.next();
      String value = (String) map.get(key);
      System.out.println(key + " = " + value);
    }
  }
}





Get file separator symbol

public class Main {
  public static void main(String[] args) {
    String dataFolder = System.getProperty("user.dir") + System.getProperty("file.separator")
        + "data";
    System.out.println("Data Folder = " + dataFolder);
  }
}





Get Java Home directory

public class Main {
  public static void main(String[] args) {
    String javaHome = System.getProperty("java.home");
    System.out.println("javaHome = " + javaHome);
  }
}





Get Java Runtime Environment (JRE) version

public class Main {
  public static void main(String[] args) {
    String version = System.getProperty("java.version");
    System.out.println("JRE Version = " + version);
  }
}





Get Java specification version using System class

public class Main {
  public static void main(String[] args) {
    System.out.println(System.getProperty("java.specification.version"));
  }
}





Get operating system name and version

public class Main {
  public static void main(String[] args) {
    String name = "os.name";
    String version = "os.version";
    String architecture = "os.arch";
    System.out.println("Name: " + System.getProperty(name));
    System.out.println("Version: " + System.getProperty(version));
    System.out.println("Architecture: " + System.getProperty(architecture));
  }
}





Get operating system temporary directory / folder

public class Main {
  public static void main(String[] args) {
    String property = "java.io.tmpdir";
    String tempDir = System.getProperty(property);
    System.out.println("OS current temporary directory is " + tempDir);
  }
}





Get path / classpath separator

import java.util.Properties;
public class Main {
  public static void main(String[] args) {
    Properties properties = System.getProperties();
    String pathSeparator = properties.getProperty("path.separator");
    System.out.println("pathSeparator = " + pathSeparator);
  }
}





Get system properties using System class

import java.util.Properties;
public class Main {
  public static void main(String[] args) {
    Properties prop = System.getProperties();
    System.out.println("Printing all System properties");
    prop.list(System.out);
  }
}





Getting the Default System Properties

public class MainClass {
  public static void main(String[] args) {
    java.util.Properties properties = System.getProperties();
    properties.list(System.out);
  }
}



-- listing properties --
java.runtime.name=Java(TM) 2 Runtime Environment, Stand...
sun.boot.library.path=C:\Java_Dev\sdk\jdk\jre\bin
java.vm.version=1.5.0-b64
java.vm.vendor=Sun Microsystems Inc.
java.vendor.url=http://java.sun.ru/
path.separator=;
java.vm.name=Java HotSpot(TM) Client VM
file.encoding.pkg=sun.io
user.country=US
sun.os.patch.level=Service Pack 2
java.vm.specification.name=Java Virtual Machine Specification
...
...
awt.toolkit=sun.awt.windows.WToolkit
java.vm.info=mixed mode
java.version=1.5.0
java.ext.dirs=C:\Java_Dev\sdk\jdk\jre\lib\ext
sun.boot.class.path=C:\Java_Dev\sdk\jdk\jre\lib\rt.jar;C:...
java.vendor=Sun Microsystems Inc.
file.separator=\
java.vendor.url.bug=http://java.sun.ru/cgi-bin/bugreport...
sun.cpu.endian=little
sun.io.unicode.encoding=UnicodeLittle
sun.desktop=windows
sun.cpu.isalist=


Get user home directory name

public class Main {
  public static void main(String[] args) {
    String userHome = "user.home";
    String path = System.getProperty(userHome);
    System.out.println("Your Home Path: " + path);
  }
}





Get username of system current user

public class Main {
  public static void main(String[] args) {
    String username = System.getProperty("user.name");
    System.out.println("username = " + username);
  }
}





Java system properties

System propertyDescriptionjava.versionJava Runtime Environment versionjava.vendorJava Runtime Environment vendorjava.vendor.urlJava vendor URLjava.homeJava installation directoryjava.vm.specification.versionJava Virtual Machine specification versionjava.vm.specification.vendorJava Virtual Machine specification vendorjava.vm.specification.nameJava Virtual Machine specification namejava.vm.versionJava Virtual Machine implementation versionjava.vm.vendorJava Virtual Machine implementation vendorjava.vm.nameJava Virtual Machine implementation namejava.specification.versionJava Runtime Environment specification versionjava.specification.vendorJava Runtime Environment specification vendorjava.specification.nameJava Runtime Environment specification namejava.class.versionJava class format version numberjava.class.pathJava class pathjava.library.pathList of paths to search when loading librariesjava.io.tmpdirDefault temp file pathjava.rupilerName of JIT compiler to usejava.ext.dirsPath of extension directory or directoriesos.nameOperating system nameos.archOperating system architectureos.versionOperating system versionfile.separatorFile separator ("/" on UNIX)path.separatorPath separator (":" on UNIX)line.separatorLine separator ("\n" on UNIX)user.nameUser"s account nameuser.homeUser"s home directoryuser.dirUser"s current working directory


Loading Native Code

public class Main {
  public static void main(String[] argv) throws Exception {
    System.loadLibrary("libraryName");
  }
}





Returns a default value if the specified property does not exist

public static String getProperty (String key, String default)



public class MainClass{
  public static void main(String[] args){
     System.out.println(System.getProperty("password","defaultPassword")); 
  }
}



OS name: Windows XP
JVM Version: 1.5.0
user.home: C:\Documents and Settings\yourname
java.home: C:\Java_Dev\sdk\jdk\jre


Set a system property

public class Main {
  public static void main(String[] argv) throws Exception {
    String previousValue = System.setProperty("application.property", "newValue");
  }
}





Setting System Properties

public class MainClass {
  public static void main(String[] a) {
    String oldValue = System.clearProperty("java.class.path");
    System.setProperty("user.dir", "C:/MyProg");
    System.out.println(System.getProperty("user.dir"));
  }
}



C:/MyProg


Setting the Value of a System Property from the Command Line: add -D option to the java command when running your program.

java -Dmy.prop="my value" MyApp

public class Main {
  public static void main(String[] argv) throws Exception {
  
    String prop = System.getProperty("my.prop");
  
  }
}





System.setProperty("apple.laf.useScreenMenuBar", "true")

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
public class Main {
  static {
    System.setProperty("apple.laf.useScreenMenuBar", "true");
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame("Menu Bar Demo");
    JMenuBar bar = new JMenuBar();
    JMenu fileMenu = new JMenu("File");
    JMenu editMenu = new JMenu("Edit");
    JMenu helpMenu = new JMenu("Help");
    bar.add(fileMenu);
    bar.add(editMenu);
    bar.add(helpMenu);
    frame.setSize(300, 150);
    frame.setJMenuBar(bar);
    frame.setVisible(true);
  }
}





Terminate a Java application

import java.io.File;
public class Main {
  public static void main(String[] args) {
    File file = new File("config.xml");
    int errCode = 0;
    if (!file.exists()) {
      errCode = 1;
    } else {
      errCode = 0;
    }
    // When the error code is not zero go terminate
    if (errCode > 0) {
      System.exit(errCode);
    }
  }
}





Using System.getProperty(String key) to retrieve system properties

  1. This method returns the value of the specified property.
  2. It returns null if the specified property does not exist.
  3. Each property comes as a key/value pair.
  4. There are system properties and user-defined properties.
  5. When a Java program runs, the JVM provides values that may be used by the program as properties.
  6. For example, the os.name system property provides the name of the operating system running the JVM.
  7. To get the value of the user.dir property, you use:



public class MainClass{
  public static void main(String[] args){
    System.out.println(System.getProperty ("user.dir"));      
  }
}



C:\Java_Dev\eclipse31\Apache Common