Java | Program Execution

05 min read

Java Sample program execution will be seen below in detail with different commands:

 

Compilation:

Z:\>javac Sample.java

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.

 

Execution:

For executing the program, give the following command to invoke the java interpreter.

Z:/>java Sample

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.

 

Rules:

  1. 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.
  2. There should not be more than one 'public' class within the same source file.
POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error