Java Tutorial/Development/System Properties
Содержание
- 1 Accessing a single System Properties: user.dir
- 2 Clear system property: The System.clearProperty() method available in Java 1.5
- 3 Create a user-defined property or change the value of the current property
- 4 Determine operating system using System class
- 5 Emit a beep by printing the ASCII Bell character to the console
- 6 Get Environment Variables
- 7 Get file separator symbol
- 8 Get Java Home directory
- 9 Get Java Runtime Environment (JRE) version
- 10 Get Java specification version using System class
- 11 Get operating system name and version
- 12 Get operating system temporary directory / folder
- 13 Get path / classpath separator
- 14 Get system properties using System class
- 15 Getting the Default System Properties
- 16 Get user home directory name
- 17 Get username of system current user
- 18 Java system properties
- 19 Loading Native Code
- 20 Returns a default value if the specified property does not exist
- 21 Set a system property
- 22 Setting System Properties
- 23 Setting the Value of a System Property from the Command Line: add -D option to the java command when running your program.
- 24 System.setProperty("apple.laf.useScreenMenuBar", "true")
- 25 Terminate a Java application
- 26 Using System.getProperty(String key) to retrieve system properties
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
- This method returns the value of the specified property.
- It returns null if the specified property does not exist.
- Each property comes as a key/value pair.
- There are system properties and user-defined properties.
- When a Java program runs, the JVM provides values that may be used by the program as properties.
- For example, the os.name system property provides the name of the operating system running the JVM.
- 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