Friday 2 December 2016

Explain the JAVA process when you run main class ?


When  you invoke a main class with the command "java MainClass", your application will start executing the main method, then it continue its execution depends on your programming logic. But before start executing the main method, there is a sequence of JAVA process involve to successfully carry its execution.

JAVA PROCESS:-

  1. Creation of  new JVM 
  2. Memory allocation to JVM.
  3. JVM loads MainClass into Memory with the help of class loader.
  4. Execution of Main Method by JVM.


Creation of  new JVM
After invoking the application with the help of command "java MainClass". The first step is java process will create new jvm with unique id to manage the process. This unique id is used to identify your process from other running process.

Memory allocation to JVM
After creating the JVM, java process will allocate the memory to JVM based on the JVM parameter set by the java user, if not it takes default value.  Default values depends on the actual environment (32 bit  or 64 bit operating system). Allocation of memory for heap, stack, class area, native area , etc are done in this step.

JVM loads MainClass into Memory with the help of class loader
Once the memory is allocated, next step is, JVM loads the MainClass into memory with the help of ClassLoader (click to read detailed explaination class loader and its execution life cycle). 

Execution of Main Method by JVM
After loading of MainClass into memory, JVM call the static main method by using class name MainClass.main(args) with the help of parameter passed by the user. JVM construct the String array based on the input given by the user while invoking the java command. In this above case its empty, So JVM calls main method with empty String array argument. After that your execution will continue depends on your business logic.

In case if JVM doesn't find main method , it throws 
"java.lang.NoSuchMethodError: main Exception in thread 'main'"


That's all about Java Process.



If you like the above solution . Please share this blog

   

No comments:

Post a Comment

s