Last Updated:

Executing | Running a Java Program

As you know, a program written in one of the high-level languages, which include the Java language, the so-called source module ("source" or "raw" in jargon, from the English "source"), cannot be immediately executed. It must first be compiled, that is, translated into a sequence of machine instructions – the object module. But it, as a rule, cannot be immediately executed: the object module must still be combined with the libraries of functions used in the module and cross-references between the sections of the object module must be allowed, resulting in a boot module - a completely ready-to-run program.

The source module written in Java cannot avoid these procedures, but here the main feature of The Java technology manifests itself - the program is compiled immediately into machine instructions, but not into the commands of any particular processor, but into the commands of the so-called Java Virtual Machine (JVM, Java Virtual Machine). A Java Virtual Machine is a collection of commands along with a system for executing them. For experts, let's say that the Java Virtual Machine is completely stacked, so that complex addressing of memory cells and a large number of registers are not required. Therefore, JVM commands are short, most of them have a length of 1 byte, which is why JVM commands are called bytecodes, although there are commands of 2 and 3 bytes in length. According to statistical studies, the average command length is 1.8 bytes. For a complete description of the commands and the entire architecture of the JVM, see the Virtual Machine Specification (VMS). If you want to know exactly how the Java Virtual Machine works, check out this specification.

Another feature of Java is that all standard functions called in the program are connected to it only at runtime, and are not included in bytecodes. According to experts, there is a dynamic layout (dynamic binding). This also greatly reduces the volume of the compiled program.

So, at the first stage, a program written in the Java language is translated by the compiler into bytecodes. This compilation is independent of the type of processor and architecture of a particular computer. It can be performed once immediately after writing the program. Bytecodes are written in one or more files, can be stored in external memory, or transmitted over a network. This is especially useful due to the small size of bytecode files. The compiled bytecodes can then be executed on any computer that has a system that implements the JVM. Neither the type of processor nor the architecture of the computer is important. This is how the Java principle is implemented: "Write once, run anywhere".

Bytecode interpretation and dynamic linking significantly slow down program execution. This does not matter in situations where bytecodes are transmitted over the network, the network is still slower than any interpretation, but in other situations a powerful and fast computer is required. Therefore, interpreters are constantly being improved in the direction of increasing the speed of interpretation. Just-In-Time compilers have been developed that remember already interpreted code fragments in the processor's machine instructions and simply execute these sections when re-accessed, for example, in loops. This greatly increases the speed of repetitive calculations. SUN has developed an entire Hot-Spot technology and incorporates it into its Java Virtual Machine. But, of course, only a specialized processor can give the highest speed.

Sun Microsystems has released PicoJava microprocessors running on the JVM instruction set, and is going to release a whole line of increasingly powerful Java processors. There are already Java-processors of other companies. These processors directly execute bytecodes. But when running Java programs on other processors, you also need to interpret the JVM instructions into the instructions of a particular processor, which means that you need an interpreter program, and for each type of processor, and for each computer architecture, you should write your own interpreter.

This task has already been solved for almost all computer platforms. They implement Java virtual machines, and for the most common platforms there are several implementations of the JVM of different companies. More and more operating systems and database management systems are incorporating the JVM implementation into their kernel. A special operating system JavaOS used in electronic devices has also been created. Most browsers have a built-in Java virtual machine for running applets.

An attentive reader has already noticed that in addition to implementing the JVM to execute bytecodes on a computer, you still need to have a set of functions called from bytecodes and dynamically composed with bytecodes. This set is packaged as a Java class library consisting of one or more packages. Each function can be written in bytecodes, but since it will be stored on a specific computer, it can be written directly to that computer's command system, thus avoiding interpreting bytecodes. Such functions are called native methods. The use of "native" methods speeds up the execution of the program.

Sun Microsystems, the creator of Java technology, distributes free of charge a set of necessary software tools for the full cycle of work with this programming language: compilation, interpretation, debugging, including a rich class library called JDK (Java Development Kit). There are sets of tools and other companies. For example, IBM's JDK is very popular.