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.
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!