Java Sample program execution will be seen below in detail with different commands:
Here 'Sample' is the name of the source file to be compiled and 'javac' is the command to invoke the java compiler, 'javac'.
After successful compilation '.class' file will be created whose name is same as that of the class name given in the program. Therefore, 'Sample.class' file will be created which will contain the bytecodes.
For executing the program, give the following command to invoke the java interpreter.
Here 'java' will load the program into Java Virtual Machine(JVM). 'Sample'is the class file name.
The file name can also be different than that of the class name. For Example, save the previous program in a file named 'firstprogram.java'. Then 'firstprogram.java' is the file name. 'Sample is the class name.
This program should be executed as follows:
Z:/>javac firstprogram.java //this will create Sample.class file
Z:/>java Sample //name of the class file
Note: If a program contains more than one class, then after compilation, those many .class files will be generated as that of classes residing in the program.
- To have different names for the class and the file, the class must have 'default' access mode. If the class is declared with 'public' access specifier, then the class name and the file name should be the same.
- There should not be more than one 'public' class within the same source file.