Topics Index

Exploring java.lang >
Siva Nookala - 01 Apr 2016
Runtime class encapsulates the run-time environment.The current runtime can be obtained from the getRuntime method.Once we obtain a reference to the current Runtime object,we can call several methods that control the state and behaviour of the JVM.Basically this class is used for memory management and executing additional processes.

The Runtime class has various methods ranging from executing processes to exiting their instances. The declaration for Runtime class is shown below:
  • public class Runtime extends Object
    Following table shows some of the commonly used methods of Runtime class
    void exit(int code)It terminates the currently running JYM by initiating its shutdown sequence.
    long freeMemory()It returns the amount of free memory in the JVM.
    long totalMemory()It returns the total amount of memory in the JVM.
    long maxMemory()It returns the maximum amount of memory that the JVM will attempt to use.
    static Runtime getRuntime()Returns the runtime object associated with the current Java application.
    void gc()It runs the garbage collector.
    Process exec(String command)It executes the specified string command in a separate process.
    int availableProcessors()Returns the number of processors available to the JVM.
    addShutdownHook(Thread thrd)Registers thrd as a thread to be run when the Java Virtual Machine terminates.
    class RuntimeDemo
        public static void main(String args[])
                Runtime Objrun=Runtime.getRuntime();//LINE A
                Process Objprocess=null;// LINE B
                    Objprocess=Objrun.exec("notepad.exe");//LINE C
                catch(Exception e)


    At LINE A we canget correct runtime by using getRuntime().At LINE B we can intialised process as null.At LINE C we executed notepad.exe command by using exec().

    • If we run the above program in our desktop or in our laptops we can get notepad window.
  • 0
    Score more than 2 points

    © meritcampus 2019

    All Rights Reserved.

    Open In App