refresh java logo

Runtime and Compile time in Java

Runtime and compile time, these are two programming terms that are more frequently used in java programming language. Both terms have it's own meanings, let us understand what these terms means in java.

compil time and runtime

Compile time

After writing the program, programmer needs to compile that program. As soon as the programmer starts compiling the program using javac compiler, the compile time gets started, and it ends when either a .class file is generated after successful compilation or an error is thrown in compilation. In other way we can say, the compilation process is referred as compile time.

For example if you wrote a program and saved it as, Once you start compiling this program using javac command as javac, the compile time gets started and it ends when a .class file as MyFirstProgram.class is generated or an error is thrown in compilation.

If the compilation is successful, compiler will generate a .class file else it throws compile time error. Compile time errors are errors that are generated at the time of compilation of program. In compilation time, the compiler checks for syntax errors, type-checking errors etc and finally generates .class file.

Does java generates .class file even if it throws compile error ?

No, it will not generate .class file, it will only display the error message in console window.

Java compiler translates the source code into another intermediate language called as Bytecode after successful compilation.

Let us see compile time error by an example. The most common mistake that beginners do is, they forget to add semicolon(;) at the end of a statement.

 class MyFirstProgram
     public static void main(String [] args) 
	    System.out.println("first statement") //missing semicolon(;)
	    System.out.println("second statement");

Once you try to compile the above program as javac, it will display an error message in console window like below.

Compile time Error: error: ';' expected
             System.out.println("first statement") //missing semicolon(;)
 1 error


As soon as the programmer starts executing the program using java command, runtime gets started and it ends when execution of program ended either successfully or unsuccessfully.

For example once you execute the command java MyFirstProgram for program given below, runtime gets started and it ends when either the output of program is generated or a runtime error is thrown.

Runtime errors are errors that are generated at the time of executing the program. If there is any error in running the program, a runtime error will be thrown.

At Runtime, the generated .class file is used for generating the output of program. It does not depend on original .java file.

What if i removed .java file after generation of .class file, can I run my program ?

Yes you can. Only .class file is used while running the program.

Let us suppose if you wrote a program like below, After compilation when you run the below program as java MyFirstProgram it will throw a runtime error.

 class MyFirstProgram 
      public static void main(String [] args)
	    int num1 = 10;
	    int num2 = 0;
	    System.out.println(num1/num2); // Runtime error: Divide by zero exception

Runtinme Error:

 Exception in thread "main" java.lang.ArithmeticException: / by zero
         at MyFirstProgram.main(

There are two types of problems that encounters while running or compiling a program that is Error and Exception. We will get to know more about this in later sections.

  • Without compilation you can not run the program, compile first then run.
  • The output of a program is generated in runtime, not in compile time.
  • If you made any changes in program after compilation, you need to compile the program again to see the changes in output.