Menu
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
    MethodDescription
    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.
    RuntimeDemo
    class RuntimeDemo
    {
        public static void main(String args[])
            {
                Runtime Objrun=Runtime.getRuntime();//LINE A
                Process Objprocess=null;// LINE B
                try
                {
                    Objprocess=Objrun.exec("notepad.exe");//LINE C
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
    }
    OUTPUT

    DESCRIPTION

    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().

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

    © meritcampus 2016 - 2017

    All Rights Reserved.