Thursday, November 13, 2025
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
32399 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6765 POSTS0 COMMENTS
Nicole Veronica
11916 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11983 POSTS0 COMMENTS
Shaida Kate Naidoo
6889 POSTS0 COMMENTS
Ted Musemwa
7141 POSTS0 COMMENTS
Thapelo Manthata
6835 POSTS0 COMMENTS
Umr Jansen
6838 POSTS0 COMMENTS