Creating and Running .java Files
This page explains how to save Java code as a text file, compile it, and run it. The file itself is simply a plain text file saved with a .java extension.
How to write a .java file
Write your Java code in a text editor and save the file with a .java extension. Save the file using the UTF-8 character encoding.
In Java, the filename and class name must always match. For example, if you define a class named Greet, the filename must be Greet.java.
sample_Greet.java
public class Greet {
public static void main(String[] args) {
String name = "Ayanami Rei";
int age = 14;
System.out.println("Hello, " + name + "!");
System.out.println("Age: " + age);
if (age >= 20) {
System.out.println("Adult.");
} else {
System.out.println("Minor.");
}
}
}
Java programs must always be written inside a class. Execution begins from the main method, which is the entry point of the program.
Greet.java
javac Greet.java java Greet
Hello, Ayanami Rei! Age: 14 Minor.
How to write comments
You can write comments (notes) in a .java file. Comments are ignored by the compiler, so they are useful for leaving descriptions or notes about the code.
| Syntax | Description |
|---|---|
| // comment | A single-line comment. Write it with one space after //. Everything from // to the end of the line is a comment. |
| /* comment */ | A multi-line comment. Everything from /* to */ is a comment. |
| /** comment */ | A Javadoc comment. Written immediately before a class or method, it is used by automatic documentation generation tools. |
sample_SampleComments.java
/**
* A sample class.
* Demonstrates how to write comments.
*/
public class SampleComments {
// A method that returns the username.
public static String getName() {
return "Ikari Shinji";
}
public static void main(String[] args) {
/*
The following outputs a greeting message.
The name is retrieved using the getName() method.
*/
String name = getName();
System.out.println("Hello, " + name + "!");
}
}
SampleComments.java
javac SampleComments.java java SampleComments
Hello, Ikari Shinji!
How to run
Because Java is a compiled language, two steps are required: "compile" and "run."
Step 1: Compile
Use the javac command to compile the .java file. If successful, a ClassName.class file is generated in the same directory. If no error message is displayed, compilation succeeded.
javac Greet.java
Step 2: Run
Use the java command with the class name (without extension) to run. Note that you specify the class name, not the filename.
java Greet
Hello, Ayanami Rei! Age: 14 Minor.
To check the version, use the following command.
java --version
Summary
A .java file is simply a plain text file. You can create one by writing Java code in a text editor and saving it with a .java extension. No special tools are needed.
Because Java is a compiled language, two steps are required: compile with javac filename.java, then run with java ClassName. Compilation produces a class file (.class), which is then executed by the Java Virtual Machine (JVM).
An important rule in Java is that the filename and the public class name must always match. For example, Greet.java must contain the Greet class. A mismatch causes a compilation error.
For information on installing Java, see Setup.
If you find any errors or copyright issues, please contact us.