Open In App

How to Execute a .class File in Java?

Last Updated : 09 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A Java Class file is a compiled java file. It is compiled by the Java compiler into bytecode to be executed by the Java Virtual Machine.

Step #1: Compile the .java File

Open Terminal (Mac) or Command Prompt (Windows). Navigate to the folder containing the java file and type the following command to compile.

javac <javaFileName>

After hitting enter, the .class file will appear in the same folder for each .java file compiled.

Step #2: Running the .class File

To run the .class file, it must have a main method in the .java file.

java <classname>

The result will be displayed in the Terminal or Command Prompt.

Example:

Java




import java.io.*;
 
class GFG {
    public static void main(String[] args) {
        // prints GFG!
        System.out.println("GFG!");
    }
}


Output

GFG!

Output in Terminal:

Run a class file in java


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads