How to Execute a .class File in Java?
Last Updated :
09 Feb, 2023
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) {
System.out.println( "GFG!" );
}
}
|
Output in Terminal:
Share your thoughts in the comments
Please Login to comment...