Procedure to develop first Java program
Step 1 :
Step 2 :
Open notepad (start > run > type notepad ) and type below code.
Step 3 :
Save this file in 01Basics folder with name Example.java
Note : Java file name can be user defined name. it is not always mandatory to save java file name with same class name. if class is declared as "public" then only it is mandatory to save the file with class name.
Let us try with different java file name.
Program development is completed. now lte us compile and execute.
Compilation and execution
Step 4 : Compilation
- Open command prompt
- Change Drive and then directory to current working directory to 01Basics folder.
- Then use javac tool to compile Example java file, as shown below.
Compiler has generated ".class" file successfully with name "FirstProgram.class" in 01Basics folder.
Note : Compiler generates ".class" file with the class name not java file name. check 01Basics folder for ".class" file name.
Step 5 :
Use java tool to execute FirstProgram class bytecodes file, as shown below.
Compiler Activity :
It takes java file name as its input and generates bytecodes for all classes defined in that java file and store each class bytecodes in seperate ".class" file with name same as class name.
JVM Activity :
It takes class name as its input and searches for a .class file with the given .class name. if it found it reads and loads that .class file bytecodes into JVM, then starts that class logic execution by calling "main" method.
What happen if compiler do if the given java file is not found ?
It throws CE: javac: file not found:
What does JVM do if the given class .class file is not found ?
It throws Runtime Error: Could find or load main class FirstProgram