Java/Development Class/System Properties
Содержание
- 1 Check Version
- 2 Clear system property: The System.clearProperty() method available in Java 1.5
- 3 Dealing with the Java environment
- 4 Determine operating system using System class
- 5 Display System Properties
- 6 Dump Props
- 7 Get Environment Variables
- 8 Get file separator symbol
- 9 Get Java Home directory
- 10 Get Java Runtime Environment (JRE) version?
- 11 Get Java specification version using System class
- 12 Get operating system name and version
- 13 Get operating system temporary directory / folder
- 14 Get path / classpath separator
- 15 Get system properties using System class
- 16 Get user home directory name
- 17 Get username of system current user
- 18 List one or more item(s) from System Properties
- 19 Machine User Name
- 20 Set a system property
- 21 Setting the Value of a System Property from the Command Line: add -D option to the java command when running your program.
- 22 System.setProperty("apple.laf.useScreenMenuBar", "true")
- 23 Use System.getenv to get environment variables
Check Version
//: com:bruceeckel:tools:CheckVersion.java
// {RunByHand}
// From "Thinking in Java, 3rd ed." (c) Bruce Eckel 2002
// www.BruceEckel.ru. See copyright notice in CopyRight.txt.
public class CheckVersion {
public static void main(String[] args) {
String version = System.getProperty("java.version");
char minor = version.charAt(2);
char point = version.charAt(4);
if(minor < "4" || point < "1")
throw new RuntimeException("JDK 1.4.1 or higher " +
"is required to run the examples in this book.");
System.out.println("JDK version "+ version + " found");
}
} ///:~
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);
}
}
Dealing with the Java environment
public class GetVersion {
public static void main(String[] args) {
System.out.println(System.getProperty("java.specification.version"));
System.out.println("System.getenv(\"PATH\") = " + System.getenv("PATH"));
}
}
Determine operating system using System class
public class Main {
public static void main(String[] args) {
System.out.println(System.getProperty("os.name"));
}
}
Display System Properties
/* From http://java.sun.ru/docs/books/tutorial/index.html */
/*
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* -Redistribution of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* -Redistribution in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
* AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
* AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
* REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
* INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
* OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
* EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed, licensed or intended
* for use in the design, construction, operation or maintenance of any
* nuclear facility.
*/
public class DisplaySystemProps {
public static void main(String[] args) {
System.getProperties().list(System.out);
}
}
Dump Props
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
public class DumpProps {
public static void main(String args[]) {
Properties props = System.getProperties();
Iterator iter = props.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
System.out.println(entry.getKey() + " --- " + entry.getValue());
}
System.out.println("-------");
Enumeration e = props.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
System.out.println(key + " --- " + props.getProperty(key));
}
}
}
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);
}
}
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);
}
}
List one or more item(s) from System Properties
/*
* Copyright (c) Ian F. Darwin, http://www.darwinsys.ru/, 1996-2002.
* All rights reserved. Software written by Ian F. Darwin and others.
* $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS""
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Java, the Duke mascot, and all variants of Sun"s Java "steaming coffee
* cup" logo are trademarks of Sun Microsystems. Sun"s, and James Gosling"s,
* pioneering role in inventing and promulgating (and standardizing) the Java
* language and environment is gratefully acknowledged.
*
* The pioneering role of Dennis Ritchie and Bjarne Stroustrup, of AT&T, for
* inventing predecessor languages C and C++ is also gratefully acknowledged.
*/
import java.io.IOException;
/**
* List one or more item(s) from System Properties
*/
public class SysPropDemo {
public static void main(String[] argv) throws IOException {
if (argv.length == 0)
System.getProperties().store(System.out, "System Properties:");
else
for (int i = 0; i < argv.length; i++) {
String s = argv[i];
System.out.println(s + " = "
+ System.getProperties().getProperty(s));
}
}
}
Machine User Name
/* From http://java.sun.ru/docs/books/tutorial/index.html */
/*
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* -Redistribution of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* -Redistribution in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
* AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
* AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
* REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
* INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
* OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
* EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed, licensed or intended
* for use in the design, construction, operation or maintenance of any
* nuclear facility.
*/
public class UserNameTest {
public static void main(String[] args) {
String name = System.getProperty("user.name");
System.out.println(name);
}
}
Set a system property
public class Main {
public static void main(String[] argv) throws Exception {
String previousValue = System.setProperty("application.property", "newValue");
}
}
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);
}
}
Use System.getenv to get environment variables
/*
* Copyright (c) 1995 - 2008 Sun Microsystems, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Sun Microsystems nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
public class Env {
public static void main (String[] args) {
for (String env: args) {
String value = System.getenv(env);
if (value != null) {
System.out.format("%s=%s%n", env, value);
} else {
System.out.format("%s is not assigned.%n", env);
}
}
}
}