Tutor HuntResources Java Resources

The Java Run-time Environment

Brief explanation of the Java Virtual Machine

Date : 30/12/2013

Author Information

Denham

Uploaded by : Denham
Uploaded on : 30/12/2013
Subject : Java

The Java run-time environment (JRE) uses a suite of software systems to create a virtual machine (VM) to emulate the real machine or operating environment on which it actually execute code for Java programmes.

Virtualisation of hardware and software systems is now a common technology and is the basis of popular systems such as Cloud Computing. It hides the actual physical characteristics of a host hardware system by presenting an alternative abstract machine to the user. The software system used to virtualised an underlying hardware system is called a hypervisor.

The JVM A Java virtual machine (JVM) is software that is implemented on virtual and non-virtual hardware and on standard operating systems. A JVM provides an environment in which Java bytecode can be executed, enabling such features as automated exception handling, which provides root-cause debugging information for every software error (exception), independent of the source code. A JVM is distributed along with a set of standard class libraries that implement the Java application programming interface (API). Appropriate APIs bundled together with JVM form the Java Runtime Environment (JRE).

One of the characteristic features of high level compiled programming language has been the portability of source programs which can be described as write once, compile anywhere programming. On the other hand, the JVM is implemented to run on many different operating system environments on many different hardware platforms. These environments all use the same JVM and therefore will run the same bytecode. Therefore, Java can be described as write once, run anywhere programming language. The Java bytecode is created by the Java translator to specifically target the JVM. The JVM does final translation of the bytecode to target a specific OS and hardware environment. Java bytecode can also be compiled from other high level programming languages that target the JVM. For example, Ada source code can be compiled to Java bytecode and executed on a JVM. Java is a trademarked name owned by Oracle but other commercial entities are allowed to Java related systems under a General License. This requires that they adhere to the certain legal obligations. Therefore JVMs are also implemented by other vendors apart from Oracle. Oracle`s JVM uses a Just in Time (JIT) compiler named HotSpot to create speed in Java program execution.

What is a Virtual Machine? This is a self-contained operating environment that is supported by a host operating system and/or hardware systems so that it behaves as a separate system to that of its host. For example, Java Applets are designed to run in the Java Virtual Machine Environment (JRE) that does not have direct access to the host operating system. The advantages of this approach are:

1) Consistency of Application behaviour: The applet will behave the same on any hardware system or the operating system of the host machine.

2) Secure interaction with the host environment: The JRE has implemented security features that that prevents and/or manages common run time error situations. Some of these error management features are actually implemented within the Java language through policy such as type checking. As can be readily deduced from the scenario illustrated in figure 2-1, there are performance penalties that associates with this approach.

The VM behaves as an isolated container with its own operating system and virtualised hardware systems that include a Random Access memory (RAM), Central Processing Unit (CPU), External Memory Units (EMU), Video Graphic Card (VGA), Network Card (NC), and other hardware systems.

Figure 2-1 An abstract layer of software systems exist between the user Java application and the host machine. The thickness of this layer also determines the time penalty of the Java system relative to other systems.

In a well defined virtual environment both the host operating system and the operating system of the virtual machine cannot tell the difference between the real physical machine and the virtual machine or other machines that may be connected on a network environment on which the VM operates. The VM is a suite of software systems that emulate the various functions of the defined VM; therefore there are certain advantages and disadvantages that result from this.

Some of these advantages include: Agile Comparability - underlying software systems that emulate hardware functionality can be quickly updated or changed for comparability with new hardware systems.

System Isolation through Encapsulation - well defined VM uses Encapsulation to enhance the isolation, security and portability of each VM on a single physical system so that each machine function like a single physical machine.

Independence from Hardware Platform - Well defined VMs are independent of any underlying physical hardware systems on which they run. Therefore, you can configure the virtual hardware components of the VM to emulate hardware components from different physical hardware systems. A VM on a given physical server can be configured to run different operating systems at the same time, for example Windows NT or Linux.

The Java Run-time Environment (JRE) As illustrated in figure 2-1, the JVM sits inside a host environment to provide an interface to Java applications that control access to resources provided by the host. Therefore, the JVM maps host resources to those available in the JVM itself to an application using the JRE. The translated source program of a Java application is stored as a portable bytecode file in a binary format with extension class. An application that consists of more than one class file can be compressed together as a single Java archive (or jar) files for easier distribution. You can run the Java class or jar file at the command prompt with use of the java subprogram. You can run web base application with use of the javaw subprogram.

The JVM runtime executes .class or .jar files, emulating the JVM instruction set by interpreting it, or using a just-in-time compiler (JIT) such as Oracle`s HotSpot. Most commercial Java translators use JIT compiling as opposed to interpreting. JIT has the advantage of greater execution speed. There are also other forms of look-ahead of time compilers that provides developers with compiler speed and other code efficiency advantages on selected host platforms.

The architecture of VMs generally relies on the use of stack-based memory models. The JVM is no exception, except that it has well designed low-level capabilities.

Other Languages that uses the JVM The specification for the JVM was originally designed to run programs compiled from Java source code. However, because of its popularity and well defined features, many other languages have developed that with translator output that targets the JVM. Some non-Java programming languages with compiler output that targets the JVM include: Erjang, Javascri pt, Free Pascal, PHP, Python, REXX, NetRexx, Ruby, Jruby, Jacl and so on!

The JVM Bytecode Verifier The JVM is a static typed system it has limited facilities for dynamic programming features. However, the Oracle JVM has introduced certain facilities since JVM 7 that provides better support for certain dynamic programming methods. The JVM is designed so that no application should access unauthorised resources, interfere inappropriately with accessible trusted code and data or crash the host machine of the JVM. The JVM ensures that memory address access is consistent and that arbitrary address values are not used in a program. In addition, common programming errors such as out of range array subscri pt values, pulling from an empty stack, use of uninitialized pointers; pushing data unto a full stack and so on are not allowed to occur. The Java language definition uses combine features such as class templates, garbage collection from the heap, and the verifier to implement these safety policies.

The verifier The JVM does special checks on bytecode before they are translated for execution. These checks consist mainly of the following checks: All braches within the program points to valid locations Variable names are always initialised with correct data The use of all references are type-safe Rigid control of access to private or package class members

Verification takes place at the point where a class is loaded onto the VM and during run-time of the bytecode. Thus, JVM is an efficient way to get memory protection on simple architectures that lack an MMU. Optimisation on code execution is achieved on the JVM for some application by obviating safety checks where it is known that the application has no safety threats.

Java Bytecode Instructions The JVM is designed to execute machine code grouped as follows: Arithmetic and Logic operation Load from memory and store to memory Subroutine call and return (function call and return) Transfer of control (branching) Creation of objects via the stack Stack management Type conversion Management of Exception Concurrency

The JVM implement these operations in the same manner on all host machines. However, the JVM must translate each operation to native machine code differently for each host machine. Hence, the syntax and semantics of the bytecode is always the same.

This resource was uploaded by: Denham

Other articles by this author