Thursday, January 22, 2026
HomeLanguagesJavaHow to Execute a .class File in Java?

How to Execute a .class File in Java?

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

RELATED ARTICLES

Most Popular

Dominic
32475 POSTS0 COMMENTS
Milvus
119 POSTS0 COMMENTS
Nango Kala
6847 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12064 POSTS0 COMMENTS
Shaida Kate Naidoo
6986 POSTS0 COMMENTS
Ted Musemwa
7220 POSTS0 COMMENTS
Thapelo Manthata
6933 POSTS0 COMMENTS
Umr Jansen
6912 POSTS0 COMMENTS