Introduction
A .java file holds source code in Java, the language behind the Java
platform, whose reference implementation is OpenJDK.
The javac compiler turns each class in it into a
.class file of bytecode for the Java
Virtual Machine. Since Java 11 a single-file program can also be run directly with
java Hello.java.
Programs and libraries are usually shipped as
.jar archives of the compiled classes
rather than as source.
Structure
A Java source file is plain text; since Java 18 the compiler reads it as UTF-8 by
default. It starts with an optional package declaration, then
import declarations, then one or more classes, interfaces, enums or
records. Only one of them may be public, and the file must be named after
it: public class Hello lives in Hello.java. The folders
normally follow the package name, so com.example.Hello is in
com/example/Hello.java.
package com.example;
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, world");
}
}
com/example/Hello.java.Links
Software
- OpenJDK, with the
javaccompiler andjavaruntime (Windows, macOS, Linux) - IntelliJ IDEA (Windows, macOS, Linux)
- Eclipse (Windows, macOS, Linux)
- Apache NetBeans (Windows, macOS, Linux)
- Visual Studio Code (Windows, macOS, Linux)
Developers
- JavaParser, Java, parses Java source into a syntax tree
- tree-sitter-java, Java grammar for the Tree-sitter parser used by editors
Reference
- The Java Language Specification, Java SE 21 edition