Run Java Program From Command Prompt
After successful compilation of
HelloWorld.class to actually run the program, we use the Java interpreter, called
java. To do so, pass the class name
HelloWorld as a command-line argument, as shown follows:
[root@host ~]# java HelloWorld
Hello World! will be printed on the screen as a result of the above command.
It is important to note that in above command we have omitted the
.class suffix of the byte-code file name (that is
HelloWorld.class in our case). The
javacommand invokes the Java Virtual Machine (will be written JVM hereafter). JVM then loads the specified class mentioned at command line and invokes the method
main of this class and start executing it, passing it a single argument that is an array of strings. The array of strings passed to
main is to receive command line arguments.
Loading of Classes and Interfaces
Loading refers to the process of finding the binary form of a class or interface type with a particular name computed from source code by a Java compiler. This is simply the bytecode. In making initial effort to execute the
main method of
HelloWorld class JVM sees that the class
HelloWorld is not loaded, means to say that JVM currently does not contain a binary representation of this class . The JVM then uses a class loader to load the class in memory, If the class file is not found at the place then an error is thrown.
Linking of Classes and Interfaces
Linking is the process of taking a binary form of a class or interface type and combining it into the run-time state of the Java virtual machine, so that it can be executed. A class or interface type is always loaded before it is linked.
HelloWorld is loaded, it must be initialized before
main is invoked. And,
HelloWorld must be linked before it is initialized. Linking involves verification,preparation, and resolution.
Verification ensures that the binary representation of a class or interface is structurally correct. For example, it checks that every instruction has a valid operation code; that every branch instruction branches to the start of some other instruction, rather than into the middle of an instruction; that every method is provided with a structurally correct signature; and that every instruction obeys the type discipline of the Java virtual machine language.
If an error occurs during verification, then an instance of the class VerifyErrorwhich is a subclass of class LinkageError will be thrown.
Preparation involves creating the static fields (class variables and constants) for a class or interface and initializing such fields to the default values. This does not require the execution of any source code; explicit initializers for static fields are executed as part of initialization, not preparation. Implementations of the Java virtual machine may precompute additional data structures at preparation time in order to make later operations on a class or interface more efficient. One particularly useful data structure is a "method table" or other data structure that allows any method to be invoked on instances of a class without requiring a search of superclasses at invocation time.
Resolution is the process of checking symbolic references from
HelloWorld to other classes and interfaces, by loading the other classes and interfaces that are mentioned and checking that the references are correct.
The resolution step is optional at the time of initial linkage. An implementation may resolve symbolic references from a class or interface that is being linked very early, even to the point of resolving all symbolic references from the classes and interfaces that are further referenced recursively. (This resolution may result in errors from these further loading and linking steps.) This implementation choice represents one extreme and is similar to the kind of "static" linkage that has been done for many years in simple implementations of the C language.
An implementation may instead choose to resolve a symbolic reference only when it is actively used; consistent use of this strategy for all symbolic references would represent the "laziest" form of resolution.
In this case, if
HelloWorld had several symbolic references to another class, then the references might be resolved one at a time, as they are used, or perhaps not at all, if these references were never used during execution of the program.
Run Java Program - Initialization
The execution of method
main of class
HelloWorld is permitted only if the class has been initialized.
Initialization consists of execution of any class variable initializers and static initializers of the class
HelloWorld, in textual order. But before
HelloWorld can be initialized, its direct superclass must be initialized, as well as the direct superclass of its direct superclass, and so on, recursively. In the simplest case,
Object as its implicit direct superclass; if class Object has not yet been initialized, then it must be initialized before
HelloWorld is initialized. Class
Object has no superclass, so the recursion terminates here. If class
HelloWorld has another class
SuperHello as its superclass, then
SuperHellomust be initialized before
HelloWorld. This requires loading, verifying, and preparing
SuperHello if this has not already been done and, depending on the implementation, may also involve resolving the symbolic references from SuperHello and so on, recursively. Initialization may thus cause loading, linking, and initialization errors, including such errors involving other types.
Run Java Program - Invoking HelloWorld.main
After completion of the initialization for class
HelloWorld (during which other consequential loading, linking, and initializing may have occurred) the method
HelloWorld is invoked.
The method main must be declared
void. It must accept a single argument that is an array of strings. Finally, A program terminates all its activity and exits when one of two things happens:
I. All the threads that are not daemon threads terminate.
II. Some thread invokes the exit method of class Runtime or class System and the exit operation is not forbidden.
JVM, JRE and JDK
Java Virtual Machine is a program that runs pre compiled Java programs, which mean JVM executes
.class files (byte-code) and produces output. The JVM is written for each platform supported by Java included in the Java Runtime Environment (JRE). The Oracle JVM is written in the C programming language. There are many JVM implementations developed by different organizations. They may somewhat differ in performance, reliability, speed and so. They can too differ in implementation specially in those features where Java specification does not mention implementation details of the feature. Garbage collection is the nice example which is left on vendor's choice and Java specification does not provide any implementation details.
JRE is an implementation of the JVM which actually executes Java programs. It includes the JVM, core libraries and other additional components to run applications and applets written in Java. Java Runtime Environment is a must install on machine in order to execute pre compiled Java Programs. JRE is smaller than the JDK so it needs less disk space and it is so because JRE does not contain Java compiler and other software tools needed to develop Java programs.
Java Development Kit is needed for developing Java applications. It is a bundle of software that is used to develop Java based applications. It includes the JRE, set of API classes, Java compiler, Webstart and additional files needed to write Java applets and applications. Conclusively, to compile and run Java program you would need JDK installed, while to run a pre compiled Java class file (byte-code) you would need JRE. JRE contains
java Java interpreter but not
javac the Java compiler