2. How to do it!!
1. Create a folder in eg: D: (D Drive) and name it eg: (myapplication)
2. Create a source file eg: (HelloWorldApp.java) in notepad
save as Text Documents(*.txt) and keep Encoding combo box as ANSI and Save.
Code
/**
* The HelloWorldApp class implements an application that
* simply prints "Hello World!" to standard output.
*/
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string.
}
}
3. Now you need to compile your file for that you need JRE
4. You need to set path varible i.e environment variable in order to run your program.
5. Open command prompt
cd D:
cd myapplication
set path=C:Program FilesJavajdk1.8.0.60bin;
javac HelloWorldApp.java
java -cp . HelloWorldApp
6. And here your program runs.
3. How actually it works!!
1. Because of Software based Virtual machine that acts as run time engine to run particular
programming language application. i.e JVM(Java Virtual Machine)
2. JVM is a part of JRE and it is responsible to load and run the java class file.
3. While working with command prompt we need to invoke the Java compiler by supplying javac
command where it comes in contact with JDK(Java Devlopment Kit)
4. JDK is a bundle software needed for developing Java Application. It has (JRE, set of API classes, Java
Compiler, Webstart and additionalfiles needed to write Java Applets and applications)
5. Java directly do not execute Java program- that job is only of JVM(Java Virtual Machine).
6. However, the Java virtual machine cannot execute .java files directly. The compiler's job is to
translate Java source files into "class files" that the virtual machine can execute.
7. Now if you need to compile multiple .java files together, then its possible by just "javac
HelloWorldApp.java one.java two.java" or "javac *.java".
8. And one more thing, Java compiler doesn't create an object file, but instead it creates a bytecode
file which is, essentially, an object file for a virtual machine.
9. However, bytecode is not an executable file.
To execute a bytecode file, you actually need to invoke a Java interpreter (called java).
Every platform has its own Java interpreter which will automatically address the platform-specific
issues that can no longer be put off.
When platform-specific operations are required by the bytecode, the Java interpreter links in
appropriate code specific to the platform.
4. Loading of Classes and Interfaces
Loading means read .class file from hard disk and store
corresponding binary data inside method area of JVM.
For each .class file JVM will store following information
1. Fully qualified name of class
2. Fully qualified name of immediate parent
3. Whether .class file represents class|interface|enum
4. Methods|Constructors|Variables information
5. Modifiers information
6. Constant Pool information
5. Linking of Classes and Interfaces
After “loading” activity JVM immediately perform Linking activity. Linking once again contain 3
activities,
1. Verification
in java .class files we never getting these alert messages.
What is the reason is inside JVM a special component is there i.e., Byte Code Verifier.
This Byte Code Verifier is responsible to verify weather .class file is properly formatted or not,
structurally correct or not, generated by valid compiler or not. If the .class file is not generated by
valid compiler then Byte Code Verifier raises runtime error java.lang.VerifyError.
This total process is done in verification activity.
2. Preparation
In preparation phase, JVM will allocate memory for class level static variables and assigned default
values.
E.g. For int ---> 0, For double ---> 0.0, For boolean ---> false
Here just default values will be assigned and original values will be assigned in initialization phase.
3. Resolution
Next phase is Resolution.
It is the process of replacing all symbolic references used in our class with original direct references
from method area.
6. Run Java Program - Initialization
• In Initialization activity, for class level static variables assigns
original values and static blocks will be executed from top to
bottom.
• While Loading, Linking and Initialization if any error occurs
then we will get runtime Exception saying
java.lang.LinkageError. Previously we discussed about
VerifyError. This is the child class of LinkageError.
7. Types of class loaders in class loader
subsystem
1. Bootstrap class loader/ Primordial class loader
Bootstrap class loader is responsible for to load classes from bootstrap class path.
Here bootstrap class path means, usually in java application internal JVM uses rt.jar.
All core java API classes like String class, StringBuilder class, StringBuffer class, java.lang packages,
java.io - enthusiast about java & open-source? packages etc are available in rt.jar.
This rt.jar path is known as bootstrap class path and the path of rt.jar is
jdk --> jre --> lib --> rt.jar
This location by default consider as bootstrap class path.
This Bootstrap class loader is responsible for loading all the classes inside this rt.jar.
This Bootstrap class loader is implemented not in java it is implemented by native languages like C,
C++ etc.
8. 2. Extension class loader
The extension class loader is the child of bootstrap class loader. This class loader is responsible to
load classes from extension class path
jdk --> jre --> lib-->ext -->*.jar
The extension class loader is responsible for loading all the classes present in the ext folder.
This Extension class loader is implemented in java only. The class name of extension class loader is
sun.misc.Launcher$ExtClassLoader.class
3. Application class loader/System class loader
The Application class loader is the child of Extension class loader.
This class loader is responsible to load classes from Application class path.
Application class path means classes in our application (Environment variable class path).
It internally uses environment vatiable path. This Application class loader is implemented in java
only.
The class name of extension class loader is
sun.misc.Launcher$AppClassLoader.class
9. Example
public class LoaderTest {
public static void main(String[] args) {
System.out.println(String.class.getClassLoader());
System.out.println(Employee123.class.getClassLoader());
System.out.println(LoaderTest.class.getClassLoader());
}
}
Output:
null // Because Bootstrap class loader is not java object it is designed with
C or C++
sun.misc.Launcher$ExtClassLoader@33909752 //Here i am created class
Employee123 and create jar for this class and put in ext folder
sun.misc.Launcher$AppClassLoader@73d16e93
Run Java Program - Invoking HelloWorld.main