Skip to content
Related Articles

Related Articles

Improve Article
How to Execute a .class File in Java?
  • Last Updated : 02 Dec, 2020

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.

1. To compile your .java files, open Terminal (Mac) or Command Prompt (Windows).

2. Navigate to the folder your java file is at.

3. To compile, type

javac <javaFileName>



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

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

java <classname>

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

Example

Java




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

Run a class file in java

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :