Sunday, November 17, 2024
Google search engine
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

Recent Comments