KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > lang > System

java.lang
Class System

java.lang.Object
  extended by java.lang.System
See Also:
Top Examples, Source Code

public static void arraycopy(Object src,
                             int srcPos,
                             Object dest,
                             int destPos,
                             int length)
See Also:
NullPointerException, ArrayStoreException, IndexOutOfBoundsException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[655]Copy Byte Array
By Anonymous on 2005/07/27 14:09:53  Rate
private byte [  ]  copyByteArray (  byte [  ]  source  )   {  
     byte [  ]  dest = new byte [  source.length  ] ; 
     System.arraycopy (  source, 0, dest, 0, source.length  ) ; 
     return dest; 
  }  
 


[1888]Copying an array fast
By Anonymous on 2007/06/10 07:14:18  Rate
Although copying an array isn't particularly difficult, it is an operation which benefits from a native implementation. Therefore java.lang.System includes a static System.arraycopy (  )  method you can use to copy one array to another. 
  
  
 public static void arraycopy ( Object source, int sourcePosition,  
  Object destination, int destinationPosition, int numberOfElements )  
  
  
 System.arraycopy (  )  copies numberOfElements elements from the array source, beginning with the element at sourcePosition, to the array destination starting at destinationPosition. The destination array must already exist when System.arraycopy (  )  is called. The method does not create it. The source and destination arrays must be of the same type. 
  
  
 For example, 
  
  
  int [  ]  unicode = new int [ 65536 ] ; 
   for  ( int i = 0; i  <  unicode.length; i++ )   {  
     unicode [ i ]  = i; 
    }  
   int [  ]  latin1 = new int [ 256 ] ; 
   System.arraycopy ( unicode, 0, latin1, 0, 256 ) ; 
 


public static String clearProperty(String key)
See Also:
SecurityManager.checkPropertiesAccess(), SecurityException, Properties, setProperty(java.lang.String, java.lang.String), getProperty(java.lang.String), IllegalArgumentException, NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static long currentTimeMillis()
See Also:
Date
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[147]Timing
By Anonymous on 2005/05/24 10:00:01  Rate
public static void main  ( String  [  ]  args )  
  {  
    // JIT/hotspot warmup: 
    for  ( int r = 0; r  <  3000; ++ r )  System.currentTimeMillis  (  ) ; 
  
  
    long time = System.currentTimeMillis  (  ) , time_prev = time; 
  
  
    for  ( int i = 0; i  <  5; ++ i )  
     {  
         // Busy wait until system time changes:  
         while  ( time == time_prev )  
              time = System.currentTimeMillis  (  ) ; 
  
  
         System.out.println  ( "delta = " +  ( time - time_prev )  + " ms" ) ; 
         time_prev = time; 
      }  
  } 


public static final PrintStream err
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[159]Additional debugging output
By Anonymous on 2003/01/30 09:38:33  Rate
//some additional debugging output: 
  
  
 Thread.dumpStack (  ) ; 
 System.err.println ( this ) ; 
 System.err.println ( Thread.currentThread (  )  ) ; 
 


[1801]function to exit program, in case of an error:
By ab on 2006/08/06 19:29:43  Rate
// function to exit program, in case of an error: 
 public static void die ( String s )   {  
     System.err.println ( "Error: ", s ) ; 
     System.exit ( 1 ) ;               
  }  
  
  
 


public static void exit(int status)
See Also:
SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static void gc()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1541]Purpose of garbage collection
By Anonymous on 2005/09/22 15:29:53  Rate
This method only request garbage collection, you can not force GC. JVM does not guarantee that GC will be started immediately

[1670]_
By Anonymous on 2005/11/04 20:23:08  Rate
The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used.

public static Map<String,String> getenv()
See Also:
ProcessBuilder.environment(), getenv(String), checkPermission, SecurityException, RuntimePermission, Object.hashCode(), Object.equals(java.lang.Object), ClassCastException, NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static String getenv(String name)
See Also:
SecurityException, RuntimePermission, checkPermission
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static Properties getProperties()
See Also:
SecurityManager.checkPropertiesAccess(), SecurityException, setProperties(java.util.Properties), getProperty(String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1706]Reading System Properties to a file
By Mohammed Zabin (mszabin { at } yahoo { dot } com) on 2006/02/06 00:31:45  Rate
This code gets system properties and store them to an exteranl file 
 input/output streams. 
 =================================================================== 
  
  
 import java.util.*; 
 import java.io.*; 
  
  
 public class SystemTest  {  
    public static void main (  String [  ]  args  )   {  
        //Current Time Milliseconds 
        System.out.println (  System.currentTimeMillis (  )   ) ; 
        System.out.println (  System.getenv ( "Foo" )  ) ; 
         
        // The Following code writes system properties  
        // to a specific file, using PrintStream and File 
        // classes 
         
        // Instantiate a property object 
        Properties prp = new Properties (  ) ; 
        //Use System class to fill the property object with its values 
        prp = System.getProperties (  ) ; 
         
        //Instantiate a PrintStream instance that takes a file 
        // instance as a paramter to write system properties 
        // to it. 
        try  {  
            prp.list (  new PrintStream (  new File ( "D:\\Debug.txt" )   )   ) ; 
         }  catch (  IOException ioe  )   {  
            System.out.println (  ioe.toString (  )   ) ; 
         }  
     }  
  }  
 


[1930]Print out all the System Properties
By Anonymous on 2007/08/16 18:36:24  Rate
Properties sysProps = System.getProperties (  ) ; 
  Enumeration en = sysProps.keys (  ) ; 
     while  (  en.hasMoreElements (  )   )  
      {  
       //add the key=value pairs 
       Object keyObj = en.nextElement (  ) ; 
       String key =  (  String  )  keyObj; 
       Object valueObj = sysProps.getProperty (  key  ) ; 
   System.out.println (  key + " : " + valueObj.toString (  )  ) ; 
      } 


public static String getProperty(String key)
See Also:
getProperties(), SecurityManager.checkPropertyAccess(java.lang.String), SecurityException, setProperty(java.lang.String, java.lang.String), IllegalArgumentException, NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[305]Get system property
By Anonymous on 2003/10/27 03:58:05  Rate
System.getProperty ( "file.separator" ) ;

[563]_
By Anonymous on 2003/12/10 17:01:42  Rate
String vers = System.getProperty ( "java.version" ) ; 
 if  ( vers.compareTo ( "1.4.1" )   <  0 )   {  
  System.out.println ( "!!!WARNING: This program must be run with a 1.4.1 or higher version VM!!!" ) ; 
  System.exit ( 1 ) ; 
  } 


[1758]Default timeouts properties in JDK 1.4
By Anonymous on 2006/05/23 11:36:33  Rate
In the 1.4 JDK, Sun has documented a pair of system properties that allow you to modify the default timeouts used by sockets. These two properties are sun.net.client.defaultConnectTimeout and sun.net.client.defaultReadTimeout, and both specify timeout values in milliseconds. 
  
  
   System.setProperty (  "sun.net.client.defaultConnectTimeout", "30000" ) ; 
   System.setProperty (  "sun.net.client.defaultReadTimeout", "30000"  ) ;


[1955]Find Current Temp Directory
By Anonymous on 2008/02/22 08:30:58  Rate
We are using getProperty ( String key )  method to find the current temp directory. The getProperty ( String key )  is defined into System class. System class extends directly Object class. System class is defined final so any class never extends System class. System class allow us to get or set system information. In this example we are pasing "java.io.tmpdir" as key to get the current temp directory into getProperty (  )  method. 
  
  
 public class TempDirExample  
  {  
     public static void main ( String [  ]  args )  
      {  
        
         System.out.println ( "OS current temporary directory is " 
  + System.getProperty ( "java.io.tmpdir" )  ) ; 
      }  
  } 


[1956]java.io.tmpdir Inconsitency
By Anonymous on 2008/02/22 08:36:19  Rate
Java has a way of giving you the path to the temp directory in a platform neutral way. You simply ask the system for the ???java.io.tmpdir??? property and you???re good to go. Here???s an example of code that does that and prints the result. 
  
  
 The Code 
  
  
 package tmp; 
 public class TempDir  {  
   public static void main ( String [  ]  args )   {  
     String tmpDir = System.getProperty ( "java.io.tmpdir" ) ; 
     System.out.println ( "java.io.tmpdir:  [ " + tmpDir + " ] " ) ; 
    }  
  }  
  
  
 The Output 
  
  
 On Windows: java.io.tmpdir: [ C:\DOCUME~1\joshua\LOCALS~1\Temp\ ]  
 On Solaris: java.io.tmpdir: [ /var/tmp/ ]  
 On Linux: java.io.tmpdir:  [ /tmp ]  
 On Mac OS X: java.io.tmpdir:  [ /tmp ]  
  
  
 As you can see, it gives you the temp directory that is appropriate to the platform. Now you just use it. Well, almost. Take a look at those paths again. The Linux and Mac OS X ones vary slightly from the others. Not much, but just enough to cause your application to fail if you don???t take precautions. You see it? Yep, the Linux and Mac OS X output don???t end in the file separator  ( forward slash ) ! So when you build paths using code like the following??? 
  
  
 String myPath = System.getProperty ( "java.io.tmpdir" )  + "some" 
   + File.separator + "place" + File.separator + "somefile.txt" ) ; 
  
  
 ???they fail. 
  
  
 On Windows:C:\DOCUME~1\joshua\LOCALS~1\Temp\some\place\somefile.txt 
 On Solaris: /var/tmp/some/place/somefile.txt 
 On Linux: /tmpsome/place/somefile.txt 
 On Mac OS X: /tmpsome/place/somefile.txt 
  
  
 tmp and some get slammed together on Linux and Mac OS X as tmpsome without the file separator between them. This means that if you want to use the java.io.tmpdir property to build a path, you have to check for the file separator at the end of the String and add it if it???s not there. 
  
  
 The other option is just to add the File.separator no matter what when building paths with java.io.tmpdir. In limited tests on Windows, Solaris, Linux and Mac OS X it looks like the operating system handles doubled file separators without problems. It simply swallows the second one and acts as if it was a single file separator.


[1992]file paths with/without file sep
By pstanton on 2009/04/07 19:54:30  Rate
if you're using a file path and you don't know if it ends with a slash, 
 the easiest way to handle that is to create files using  
  
  
 new File ( someunknownpath, "filename.txt" ) ; 
  
  
 that way, if someunknownpath does or doesn't end with a file sep it 
 will be handled correctly every time. 
  
  
 if you just want to get a path and not a file object use 
  
  
 new File ( someunknownpath, "filename.txt" ) .getAbsolutePath (  ) ; 
  
  
 the overhead should be minimal. 
 


public static String getProperty(String key,
                                 String def)
See Also:
getProperties(), SecurityManager.checkPropertyAccess(java.lang.String), setProperty(java.lang.String, java.lang.String), IllegalArgumentException, NullPointerException, SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1935]Setting the Value of a System Property
By Anonymous on 2007/10/30 17:32:22  Rate
// Get a system property 
 String dir = System.getProperty ( "user.dir" ) ; 
      
 // Set a system property 
 String previousValue = System.setProperty ( "application.property", "newValue" ) ;


public static SecurityManager getSecurityManager()
See Also:
setSecurityManager(java.lang.SecurityManager)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static int identityHashCode(Object x)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final InputStream in
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1158]Read input from keyboard
By Anonymous on 2005/04/25 11:33:25  Rate
int rows = 0, cols = 0; 
 String tmp; 
 System.out.print ( name + ": how many rows? " ) ; 
 tmp =  ( new BufferedReader ( new InputStreamReader ( System.in )  )  ) .readLine (  ) ; 
 rows = Integer.parseInt ( tmp ) ; 
  
  
 System.out.print ( name + ": how many cols? " ) ; 
 tmp =  ( new BufferedReader ( new InputStreamReader ( System.in )  )  ) .readLine (  ) ; 
 cols = Integer.parseInt ( tmp ) ; 
  
  
 String [  ]  [  ]  strTable = new String [ rows ]  [ cols ] ; 
 


[1802]read a line from STDIN (=System.in)
By ab on 2006/08/06 19:35:21  Rate
// read a line from STDIN  ( =System.in )  
 BufferedReader in = new BufferedReader ( new InputStreamReader ( System.in )  ) ; 
 String line; 
 line=in.readLine (  ) ; 
 


public static Channel inheritedChannel()
                                throws IOException
See Also:
SecurityException, SelectorProvider
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static void load(String filename)
See Also:
SecurityManager.checkLink(java.lang.String), NullPointerException, UnsatisfiedLinkError, SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static void loadLibrary(String libname)
See Also:
SecurityManager.checkLink(java.lang.String), NullPointerException, UnsatisfiedLinkError, SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static String mapLibraryName(String libname)
See Also:
ClassLoader.findLibrary(java.lang.String), loadLibrary(java.lang.String), NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static long nanoTime()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static final PrintStream out
See Also:
PrintStream.println(java.lang.String), PrintStream.println(java.lang.Object), PrintStream.println(long), PrintStream.println(int), PrintStream.println(float), PrintStream.println(double), PrintStream.println(char[]), PrintStream.println(char), PrintStream.println(boolean), PrintStream.println()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static void runFinalization()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


@Deprecated
public static void runFinalizersOnExit(boolean value)
See Also:
SecurityManager.checkExit(int), Runtime.gc(), Runtime.exit(int), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static void setErr(PrintStream err)
See Also:
RuntimePermission, SecurityManager.checkPermission(java.security.Permission), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1539]Direct error messages to a file
By Anonymous on 2005/09/22 14:47:49  Rate
// direct messages to a file 
 Stream st = new Stream ( new FileOutputStream ( "systemerr.txt" )  ) ;  
 System.setErr ( st ) ;  
 


public static void setIn(InputStream in)
See Also:
RuntimePermission, SecurityManager.checkPermission(java.security.Permission), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static void setOut(PrintStream out)
See Also:
RuntimePermission, SecurityManager.checkPermission(java.security.Permission), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1540]Direct console messages to a file
By Anonymous on 2005/09/22 15:10:03  Rate
// direct messages to a file 
 Stream st = new Stream ( new FileOutputStream ( "systemout.txt" )  ) ;  
 System.setOut ( st ) ;


public static void setProperties(Properties props)
See Also:
SecurityManager.checkPropertiesAccess(), SecurityException, getProperties(), getProperty(String)
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static String setProperty(String key,
                                 String value)
See Also:
SecurityManager.checkPermission(java.security.Permission), PropertyPermission, getProperty(java.lang.String, java.lang.String), getProperty(java.lang.String), IllegalArgumentException, NullPointerException, SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public static void setSecurityManager(SecurityManager s)
See Also:
RuntimePermission, SecurityManager.checkPermission(java.security.Permission), getSecurityManager(), SecurityException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  

Popular Tags