Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Tuesday 18 July 2017

Java Runtime

Java Runtime Class

Java Runtime

In java Runtime is a class which is belong to the java.lang package. Every java application has a single instance of a class Runtime that allows the application to interface with the environment in which the application is running

The current runtime can be obtained from the getRuntime method and an application cannot create its own instance of this class.

Runtime class in java extends only super class of java i.e Object class.

public class Runtime extends Object 


Methods of Java Runtime Class

There are some methods of java Runtime class and these are given below.

(1) void addShutdownHook(Thread hook)

This method registers a new virtual-machine shutdown hook.

(2) int availableProcessors()

This method returns the number of processors available to the Java Virtual Machine.

(3) Process exec(String command)

This method executes the specified string command in a separate process.

(4) Process exec(String[] cmdarray)

This method executes the specified command and arguments in a separate process.

(5) Process exec(String command, String[] envp)

This method executes the specified string command  in a separate process with the specified environment.

(6) void exit(int status)

This method terminates the currently running Java Virtual Machine by initiating its shut down sequence.

(7) long freeMemory()

It is used to returns the amount of free memory in the java virtual machine.

(8) void gc()

It is used to runs the garbage collector.

(9) static Runtime getRuntime()

This method returns the run time object associated with the current Java application.

(10) long maxMemory()

This method returns the maximum amount of memory that the java virtual machine will attempt to use.

(11) void halt(int status)

Java virtual machine forcibly terminates by this method.

(12) void load(String filename)

This method loads the native library specified by the filename argument.

(13) boolean removeShutdownHook(Thread hook)

This method De-registers a previously-registers virtual machine shut down hook.

(14) long totalMemory()

This method returns the total amount of memory in the java virtual machine.



(1) Java Runtime exec() Method Example

By the help of this program, we will open notepad in java.

class Test
{
public static void main(String args[])throws Exception
{
Runtime.getRuntime().exec("notepad");
}
}

(2) Java Runtime availableProcessors() Method Example

public class Sample
{
public static void main(String args[])throws Exception
{
System.out.println(Runtime.getRuntime().availableProcessors());
}
}

(3) Java Runtime exit() Method Example

public class ExitExample
{
public static void main(String args[])
{
//program start here
System.out.println("program start");

//cause the program to exit
Runtime.getRuntime().exit(0);

//try to print after exit method
System.out.println("program will continue");
}
}

output : program start


(4) Java freeMemory() Method Example

public class Demo
{
public static void main(String args[])
{
//print when the program starts
System.out.println("program starts here");

//print the numbers of free bytes
System.out.println(Runtime.getRuntime().freeMemory());
}
}

output : program starts here
              57645872

(5) Java maxMemory() Method Example

public class Demo1
{
public static void main(String args[])
{
//print when the program starts
System.out.println("program starts here");

//print the numbers of free bytes
System.out.println(Runtime.getRuntime().maxMemory());
}
}

output : program starts here
              934281216

(6) Java totalMemory() Method Example

public class Demo1
{
public static void main(String args[])
{
//print when the program starts
System.out.println("program starts here");


System.out.println(Runtime.getRuntime().totalMemory());
}
}

output : program starts here

Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate