What is the difference between javac, java commands?


The javac command is used to compile Java programs, it takes .java file as input and produces bytecode. Following is the syntax of this command.

>javac sample.java

The java command is used to execute the bytecode of java. It takes byte code as input and runs it and produces the output. Following is the syntax of this command.

>java sample

Let us consider an example create a Sample Java program with name Sample.java

Sample.java

public class Sample {
   public static void main(String args[]) {
      System.out.println("Hi welcome to Tutorialspoint");      
   }
}

If you compile this program using Javac command as shown below -

C:\Examples >javac Sample.java

This command compiles the given java file and generates a .class file (byte code)

Sample Java File

And if you execute the generated byte code (.class) file using the java command as shown below -

C:\Sample>java Sample
Hi welcome to Tutorialspoint

This command executes the given .class file and produces the respective output.

Updated on: 30-Jul-2019

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements