Java/Development Class/System Properties

Материал из Java эксперт
Перейти к: навигация, поиск

Check Version

   <source lang="java">

//: 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");
 }

} ///:~


 </source>
   
  
 
  



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

   <source lang="java">


public class Main {

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

}


 </source>
   
  
 
  



Dealing with the Java environment

   <source lang="java">

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"));    
 }

}


 </source>
   
  
 
  



Determine operating system using System class

   <source lang="java">

public class Main {

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

}

 </source>
   
  
 
  



Display System Properties

   <source lang="java">

/* 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);
   }

}


 </source>
   
  
 
  



Dump Props

   <source lang="java">

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));
   }
 }

}


 </source>
   
  
 
  



Get Environment Variables

   <source lang="java">


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);
   }
 }

}

 </source>
   
  
 
  



Get file separator symbol

   <source lang="java">

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);
 }

}

 </source>
   
  
 
  



Get Java Home directory

   <source lang="java">


public class Main {

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

}

 </source>
   
  
 
  



Get Java Runtime Environment (JRE) version?

   <source lang="java">


public class Main {

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

}


 </source>
   
  
 
  



Get Java specification version using System class

   <source lang="java">

public class Main {

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

}

 </source>
   
  
 
  



Get operating system name and version

   <source lang="java">

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));
 }

}


 </source>
   
  
 
  



Get operating system temporary directory / folder

   <source lang="java">

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);
 }

}

 </source>
   
  
 
  



Get path / classpath separator

   <source lang="java">

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);
 }

}

 </source>
   
  
 
  



Get system properties using System class

   <source lang="java">

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);
 }

}

 </source>
   
  
 
  



Get user home directory name

   <source lang="java">

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);
 }

}

 </source>
   
  
 
  



Get username of system current user

   <source lang="java">


public class Main {

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

}

 </source>
   
  
 
  



List one or more item(s) from System Properties

   <source lang="java">

/*

* 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));
     }
 }

}



 </source>
   
  
 
  



Machine User Name

   <source lang="java">

/* 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);
   }

}


 </source>
   
  
 
  



Set a system property

   <source lang="java">

public class Main {

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

}

 </source>
   
  
 
  



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

   <source lang="java">

java -Dmy.prop="my value" MyApp

public class Main {

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

}

 </source>
   
  
 
  



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

   <source lang="java">

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);
 }

}

 </source>
   
  
 
  



Use System.getenv to get environment variables

   <source lang="java">

/*

* 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);
           }
       }
   }

}

 </source>