Java run time system

Java Tutorial/Development/Runtime System

Display the amount of free memory in the Java Virtual Machine.

import java.text.DecimalFormat; public class Main

public static void main(String[] args) < DecimalFormat df = new DecimalFormat("0.00"); long freeMem = Runtime.getRuntime().freeMemory(); System.out.println(df.format(freeMem / 1000000F) + " MB"); >

Display the maximum amount of memory

import java.text.DecimalFormat; public class Main

public static void main(String[] args) < DecimalFormat df = new DecimalFormat("0.00"); long maxMem = Runtime.getRuntime().maxMemory(); System.out.println(df.format(maxMem / 1000000F) + " MB"); >

Display the total amount of memory in the Java virtual machine.

import java.text.DecimalFormat; public class Main

public static void main(String[] args) < DecimalFormat df = new DecimalFormat("0.00"); long totalMem = Runtime.getRuntime().totalMemory(); System.out.println(df.format(totalMem / 1000000F) + " MB"); >

Execute a command from code

public static void main(String[] argv) throws Exception < // Execute a command without arguments String command = "ls"; Process child = Runtime.getRuntime().exec(command); // Execute a command with an argument command = "ls /tmp"; child = Runtime.getRuntime().exec(command); >

Execute a command with more than one argument

public static void main(String[] argv) throws Exception < // Execute a command with an argument that contains a space String[] commands = new String[] < "grep", "hello world", "/tmp/f.txt" >; commands = new String[] < "grep", "hello world", "c:\\f.txt" >; Process child = Runtime.getRuntime().exec(commands); >

Execute system command

import java.io.IOException; public class Main

public static void main(String[] args) throws IOException < String cmd = "cmd.exe /c start "; // String file = "c:\\version.txt"; // String file nofollow" href="http://www.google.ru">http://www.google.ru"; // String file = "c:\\"; // String file nofollow" href="mailto:author@my.ru">mailto:author@my.ru"; String file = "mailto:"; Runtime.getRuntime().exec(cmd + file); >

From Runtime.exec() to ProcessBuilder

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Arrays; public class Main

public static void main(String args[]) throws IOException < Runtime runtime = Runtime.getRuntime(); Process process = runtime.exec(args); InputStream is = process.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line; System.out.printf("Output of running %s is:", Arrays.toString(args)); while ((line = br.readLine()) != null) < System.out.println(line); >>

Get Number of Available Processors

public static void main(String[] args)

Читайте также:  Python file type open

> // Number of processors available to the Java Virtual Machine: 2

Launch a Unix script with Java

public static void main(String[] argv) throws Exception < String[] cmd = < "/bin/sh", "-c", "ls >hello" >; Runtime.getRuntime().exec(cmd); >

Milliseconds elapsed since January 1, 1970

import java.util.Properties; public class SystemApp
public static void main(String args[])

Read output from a Command execution

import java.io.InputStream; public class Main

public static void main(String[] argv) throws Exception < String command = "ls"; Process child = Runtime.getRuntime().exec(command); InputStream in = child.getInputStream(); int c; while ((c = in.read()) != -1) < System.out.println((char) c); >in.close(); >

Registering Shutdown Hooks for Virtual Machine

public class Main implements Runnable
public void run() < System.out.println("Shutting down"); >public static void main(String[] arg)

Send an Input to a Command

import java.io.OutputStream; public class Main
public static void main(String[] argv) throws Exception

System.getProperty

public static void main(String args[])

> The following properties are available: file.separator java.specification.version java.vm.version java.class.path java.vendor line.separator java.class.version java.vendor.url os.arch java.rupiler java.version os.name java.ext.dirs java.vm.name os.version java.home java.vm.specification.name path.separator java.io.tmpdir java.vm.specification.vendor user.dir java.library.path java.vm.specification.version user.home java.specification.name java.vm.vendor user.name java.specification.vendor

Timing program execution.

public static void main(String args[]) < long start, end; System.out.println("Timing a for loop from 0 to 1,000,000"); start = System.currentTimeMillis(); // get starting time for (int i = 0; i < 1000000; i++) ; end = System.currentTimeMillis(); // get ending time System.out.println("Elapsed time: " + (end - start)); >

Using arraycopy().

public static void main(String args[]) < Runtime r = Runtime.getRuntime(); Process p = null; try < p = r.exec("notepad"); p.waitFor(); >catch (Exception e) < System.out.println("Error executing notepad."); >System.out.println("Notepad returned " + p.exitValue()); >

Источник

Java Runtime class

Java Runtime class

The class used for the interaction with the run time environment of Java is called run time class in Java which provides several methods such as Runtime.getRuntime(), exit(int status), addShutdownHook(Thread hook), Process exec(String command) throws an input-output exception, availableProcessors(), freeMemory(), totalMemory(), etc. for process execution, for invoking GC, to obtain the total memory, to obtain the free memory, etc. whose declaration is public class Runtime extends object and only one application of Java can use only one instance of java.lang.Runtime class and a singleton instance of Runtime class is returned using Runtime.getRuntime() method in Java.

Web development, programming languages, Software testing & others

Methods of Java Runtime class

There are several methods of Java Runtime Class. They are:

1. Runtime getRuntime()

An instance of the Run time class is returned using the Runtime getRuntime() method. Consider the below Java program to demonstrate the Runtime getRuntime() method of the Java Run time class.

//a class called program is defined public class program < //main method is called public static void main(String[] args) < // The current runtime in association with process is obtained using getRuntime method. Runtime roll = Runtime.getRuntime(); // The current free memory for the current runtime is obtained using freeMemory() method. System.out.println("The current free memory for the current runtime is" + roll.freeMemory()); >>

Java Runtime Class Example 1

Explanation: In the above program, a class called program is defined. Then the main method is called. Then The current runtime in association with the process is obtained using getRuntime method. Then The current free memory for the current runtime is obtained using freeMemory() method.

2. exit(int status)

The current virtual machine is terminated using the Runtime exit(int status)method. Consider the below Java program to demonstrate the exit(int status) method of the Java Run time class.

//a class called program is defined public class program < //main method is called public static void main(String[] args) < //the current runtime or program is caused to exit using exit(int status) method Runtime.getRuntime().exit(0); //This statement is not executed because the program is terminated by the usage of exit(int status) method above System.out.println("Checking if the program executes this statement depsite the use of exit(int status) method"); >>

Java Runtime Class Example 2

The output of the above program is blank.

Explanation: In the above program, a class called program is defined. Then the main method is called. Then the current runtime or program is caused to exit using the exit(int status) method. Then the statement is not executed because the program is terminated by the usage of the exit(int status) method in the above statement.

3. addShutdownHook(Thread Hook)

A new hook thread is registered usingaddShutdownHook(Thread Hook)method. Consider the below Java program to demonstrate the addShutdownHook(Thread Hook)method of the Java Run time class.

//a class called program is defined public class program < // when the program is about to exit, this class extending the thread is called static class Mess extends Thread < public void run() < System.out.println("The program is going to exit"); >> //main method is called public static void main(String[] args) < try < //the class mess is registered as shut down hook Runtime.getRuntime().addShutdownHook(new Mess()); //The thread is made to sleep for certain seconds System.out.println("Five seconds of waiting time for the program to exit"); Thread.sleep(5); >catch (Exception ex) < ex.printStackTrace(); >> >

Java Runtime Class Example 3

Explanation: In the above program, a class called program is defined. When the program is about to exit, this class extending the thread is called. Then the main method is called. Then the class mess is registered as shut down the hook. Then the thread is made to sleep for certain seconds.

4. Process exec(String command) throws IOException

The given command is executed in a separate process using Process exec(String command) throws IOExceptionmethod. Consider the below Java program to demonstrate Process exec(String command) throws IOExceptionmethod of Java Run time class

//a class called program is defined public class program < //main method is called public static void main(String[] args) < try < // a process is created to execute google chrome Process proc = Runtime.getRuntime().exec("google-chrome"); System.out.println("Successfully started google chrome"); >catch (Exception e) < e.printStackTrace(); >> >

Java Runtime Class Example 4

Explanation: In the above program, a class called program is defined. Then the main method is called. Then a process is created to execute google chrome.

5. availableProcesors()

The number of available processors are returned using availableProcesors()method. Consider the below Java program to demonstrate the availableProcessors() method of the Java Run time class.

//a class called program is defined public class program < //main method is called public static void main(String[] args) < //checking for the number of available processors using availableprocessors() method. System.out.println("The number of available processors are " + Runtime.getRuntime().availableProcessors()); >>

availableProcesors() Example 5

Explanation: In the above program, a class called program is defined. Then the main method is called. Then a check for the number of available processors is done using availableprocessors() method.

6. freeMemory()

The amount of free memory is returned in JVM using freeMemory()method.

Consider the below Java program to demonstrate freeMemory() method of the Java Run time class.

//a class called program is defined public class program < //main method is called public static void main(String[] args) < //The amount of free memory available is given by using freememory() method System.out.println("The amount of free memory available is " + Runtime.getRuntime().freeMemory()); >>

freeMemory() Example 6

Explanation: In the above program, a class called program is defined. Then the main method is called. Then the amount of free memory available is given by using freememory() method.

7. totalMemory()

The amount of total memory is returned in JVM using totalMemory()method. Consider the below Java program to demonstrate totalMemory() method of the Java Run time class.

//a class called program is defined public class program < //main method is called public static void main(String[] args) < //The amount of total memory available is given by using totalmemory() method System.out.println("The amount of free memory available is " + Runtime.getRuntime().totalMemory()); >>

totalMemory() Example 7

Explanation: In the above program, a class called program is defined. Then the main method is called. Then the amount of total memory available is given by using totalmemory() method.

This is a guide to the Java Runtime class. Here we discuss the concept of Java Runtime class through definition and their methods along with programming examples and their outputs. You can also go through our other suggested articles to learn more –

500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access

1000+ Hours of HD Videos
43 Learning Paths
250+ Courses
Verifiable Certificate of Completion
Lifetime Access

1500+ Hour of HD Videos
80 Learning Paths
360+ Courses
Verifiable Certificate of Completion
Lifetime Access

3000+ Hours of HD Videos
149 Learning Paths
600+ Courses
Verifiable Certificate of Completion
Lifetime Access

All in One Software Development Bundle 3000+ Hours of HD Videos | 149 Learning Paths | 600+ Courses | Verifiable Certificate of Completion | Lifetime Access

Financial Analyst Masters Training Program 1000+ Hours of HD Videos | 43 Learning Paths | 250+ Courses | Verifiable Certificate of Completion | Lifetime Access

Источник

Java run time system

Learn Latest Tutorials

Splunk tutorial

SPSS tutorial

Swagger tutorial

T-SQL tutorial

Tumblr tutorial

React tutorial

Regex tutorial

Reinforcement learning tutorial

R Programming tutorial

RxJS tutorial

React Native tutorial

Python Design Patterns

Python Pillow tutorial

Python Turtle tutorial

Keras tutorial

Preparation

Aptitude

Logical Reasoning

Verbal Ability

Company Interview Questions

Artificial Intelligence

AWS Tutorial

Selenium tutorial

Cloud Computing

Hadoop tutorial

ReactJS Tutorial

Data Science Tutorial

Angular 7 Tutorial

Blockchain Tutorial

Git Tutorial

Machine Learning Tutorial

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures tutorial

DAA tutorial

Operating System

Computer Network tutorial

Compiler Design tutorial

Computer Organization and Architecture

Discrete Mathematics Tutorial

Ethical Hacking

Computer Graphics Tutorial

Software Engineering

html tutorial

Cyber Security tutorial

Automata Tutorial

C Language tutorial

C++ tutorial

Java tutorial

.Net Framework tutorial

Python tutorial

List of Programs

Control Systems tutorial

Data Mining Tutorial

Data Warehouse Tutorial

Javatpoint Services

JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.

  • Website Designing
  • Website Development
  • Java Development
  • PHP Development
  • WordPress
  • Graphic Designing
  • Logo
  • Digital Marketing
  • On Page and Off Page SEO
  • PPC
  • Content Development
  • Corporate Training
  • Classroom and Online Training
  • Data Entry

Training For College Campus

JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week

Like/Subscribe us for latest updates or newsletter RSS Feed Subscribe to Get Email Alerts Facebook Page Twitter Page YouTube Blog Page

Источник

Оцените статью