File Extension File Extension Guide

What is a JAVA File?Source Code

A guide to the .java extension, a Java source code file. What a JAVA file holds, how the javac compiler turns it into bytecode, how the Java Virtual Machine runs it, and how to open one.

Java Source Code ☕ Java Language 📝 Plain Text ⚙️ Compiled to Bytecode
.JAVA

Java Source Code File

Type:Source code
Extension:.java
Language:Java
Compiled By:javac
Runs On:Java Virtual Machine

☕ What is a JAVA File?

A JAVA file is where a programmer keeps the Java code they are writing, a plain-text file of instructions for a program built in the Java language. It holds human-readable instructions, not something you run directly, but the starting point that gets turned into a working program.

Java is one of the most widely used programming languages in the world, running everything from Android apps to huge business systems. A .java file is where that code lives while it is being written. Open one and you see ordinary text, the classes, methods and statements a developer has typed out according to the rules of the Java language. On its own this text does nothing, in the same way a recipe written on paper does not cook a meal. It has to be processed first, by a tool called a compiler, before a computer can actually run it.

That two-step nature is the key thing to understand about JAVA files. You write the code as a .java file, then a Java compiler converts it into a different form the machine can execute. Because of this, a .java file is something you open to read or edit rather than to view or play, and it belongs to the world of software development rather than everyday documents. The rest of this guide walks through what is inside one, how it becomes a running program and how to open it.

Key Characteristics

  • Plain text a developer can read
  • Coded in the Java programming language
  • Compiled before it can run
  • The source a developer writes and edits

Good to Know

  • Extension is .java
  • Compiled by the javac tool
  • Becomes a .class bytecode file
  • Runs on the Java Virtual Machine
💡 Why it matters: A JAVA file is the plain-text source code of a Java program, written by a developer and later compiled into a runnable form. You open it to read or edit the code, not to run it directly.

⚡ Quick Facts

File Extension.java
File TypeJava source code file
LanguageJava
FormatPlain text
Compiled Byjavac (the Java compiler)
Compiles To.class bytecode file
Runs OnJava Virtual Machine (JVM)
NeedsJava Development Kit (JDK)

🧱 What is Inside a JAVA File?

Opening a JAVA file reveals plain, structured text, and while it can look busy at first, it is built from a few clear pieces once you know what to look for.

At the heart of almost every JAVA file is a class, a named block that groups together related code, written with the keyword class followed by a name. Inside the class are methods, which are named chunks of instructions that do a particular job, and the most important of these is often main, the special method where a program begins running. Statements inside the methods do the actual work, such as the familiar line that prints a message to the screen. A file usually starts with a package line saying where the code belongs, and import lines that pull in ready-made code from Java's libraries so you do not have to write everything from scratch. The syntax leans on curly braces to mark where blocks begin and end, and semicolons to close each statement. Here is a short example showing the shape of a simple JAVA file.

A simple JAVA file (Hello.java)

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}

That small file defines a class called Hello with a main method that prints a greeting. Real JAVA files can run to hundreds of lines with many methods and classes, but they are all assembled from these same building blocks of classes, methods and statements.

⚙️ From Source to Bytecode

A JAVA file cannot run as it is, and understanding what happens to it is the single best way to make sense of how Java works, because the journey from text to running program has a distinctive twist.

The first step is compiling. A tool called javac, the Java compiler, reads your .java source file and translates it into a new file with a .class extension. What it produces is not the machine code a processor runs directly, but an in-between form called bytecode, a compact set of instructions meant for Java's own runtime rather than for any particular chip. Each class in your source becomes its own .class file, so a file called Hello.java compiles into Hello.class. This is where a JAVA file stops being just text and becomes something ready to execute. The bytecode still needs one more stage to actually run, which happens on the Java Virtual Machine, but compiling is the moment the code you wrote is checked for mistakes and turned into the portable form that makes Java so flexible. If the compiler finds errors in your source, it reports them here rather than producing a .class file, which is why fixing compile errors is a familiar part of writing Java.

Write once, run anywhere: one file, compiled once, runs everywhere Hello.java Java source code javac Hello.class bytecode JVM Java Virtual Machine runs the bytecode The same Hello.class runs on any system with a JVM 🪟 Windows 🍎 macOS 🐧 Linux
✅ In short: The javac compiler turns your .java source into a .class file of bytecode. Bytecode is a portable in-between form, not the machine code a processor runs directly, which is the detail that makes Java run on so many systems.

🌍 The JVM and Run Anywhere

Once your code is compiled into bytecode, one more piece brings it to life, and it is the piece that gives Java its famous promise of running the same program almost anywhere.

That piece is the Java Virtual Machine, usually shortened to JVM. It is a program that sits on top of your operating system and knows how to run bytecode, acting as a translator between the portable .class files and the real machine underneath. When you launch a Java program, the JVM reads the bytecode and, using an approach called just-in-time compilation, turns it into the native machine instructions your processor understands, doing so as the program runs. The clever result is that the same .class file can run on Windows, macOS, Linux or anywhere else, as long as that system has a JVM installed. This is the idea captured in Java's well-known slogan, write once and run anywhere, since a developer compiles the code just once into bytecode and every different computer handles the final step through its own JVM. It is a different approach from languages like C, where a program is compiled all the way down to an executable built for one specific type of computer, and moving to another kind means compiling again.

▶️ How to Compile and Run a JAVA File

Once you have written a JAVA file and installed the tools, turning it into a running program takes just two short commands, and seeing them makes the compile-then-run idea concrete.

You need the Java Development Kit installed first, since it provides the two commands that do the work. The first is javac, the compiler, which you point at your source file to produce the bytecode. The second is java, the launcher, which starts the Java Virtual Machine and runs the compiled class. So for a file called HelloWorld.java you compile it, which creates HelloWorld.class, and then run that class by name. Notice that when you run it you leave off the extension, giving just the class name rather than the file name. Here are the two steps for a simple program.

Compile, then run

# 1. Compile the source into bytecode
javac HelloWorld.java
# this creates HelloWorld.class

# 2. Run the compiled class (no .class on the end)
java HelloWorld
# output: Hello, world!

Newer versions of Java add a handy shortcut for quick tests. Since Java 11 you can run a program held in a single file straight away with java HelloWorld.java, and it compiles and runs in one step behind the scenes, which is convenient for small experiments. There is also jshell, an interactive tool added in Java 9 that lets you type Java statements one at a time and see the result immediately, without creating a file at all. For anything larger than a quick trial, though, the compile-then-run pair of javac and java remains the standard way to build and launch a Java program.

📦 JDK vs JRE vs JVM

Three initials come up constantly around Java, the JDK, the JRE and the JVM, and they confuse a lot of people, yet they fit together neatly once you see that each one contains the last.

The innermost piece is the JVM, the Java Virtual Machine, the engine that actually runs bytecode by translating it into native machine instructions and handling jobs like memory management as the program runs. Wrapped around it is the JRE, the Java Runtime Environment, which is the JVM plus the standard code libraries a program relies on, so the JRE is everything you need to run a Java application but not to build one. Around that sits the JDK, the Java Development Kit, which is the JRE plus the developer tools, including the javac compiler, the jar packaging tool and a debugger, so the JDK is what you install to write and compile Java rather than just run it. The simple way to remember it is that the JDK contains the JRE, and the JRE contains the JVM. Which one you need depends on your goal, since someone who only wants to run a Java program needs the JRE, while anyone writing Java, and therefore compiling .java files, needs the full JDK.

The JDK contains the JRE, which contains the JVM JDK Java Development Kit, adds javac, jar, debugger JRE Java Runtime Environment, adds core libraries JVM Java Virtual Machine runs the bytecode, turning it into native machine code as the program runs the engine at the core of it all
ComponentWhat it isYou need it to
JVMThe engine that runs bytecodeExecute a compiled program
JREJVM plus core librariesRun Java applications
JDKJRE plus developer toolsWrite and compile Java code

📛 The File Naming Rule

There is one rule about JAVA files that trips up almost everyone starting out, and knowing it early saves a lot of confusion, because Java is strict about how a file is named.

The rule is that a JAVA file must be named after the public class it contains, matching it exactly. If your file defines a public class called Invoice, the file has to be saved as Invoice.java, with the same capitalisation, and a file can hold only one public class. Java is case-sensitive, so Invoice and invoice are treated as different names, and the convention is for class names to begin with a capital letter. Get this wrong and the compiler simply refuses to build the file, reporting that the public class and the file name do not match. The reason behind the rule is that it lets the compiler and the Java runtime find a class reliably just from its name, since the name of the class tells them exactly which file to look in. So when you create a JAVA file, the safe habit is to decide the public class name first and give the file precisely that name followed by .java.

📂 How to Open a JAVA File

Opening a JAVA file splits into two quite separate goals, simply reading its code versus working with it to build a program, and the tool you reach for follows from that.

Because a JAVA file is plain text, any text editor will open it so you can read or edit the code. The Notepad app on Windows or TextEdit on a Mac shows the contents fine, though a coding editor like Visual Studio Code or Sublime Text makes it much easier to follow by colour-coding the different parts. For real development work, most people use an integrated development environment, or IDE, a program built for writing software that combines an editor with the tools to compile and run it. Popular choices for Java are IntelliJ IDEA, Eclipse and NetBeans, along with Visual Studio Code once its Java extensions are added. To go beyond reading and actually compile and run the code, you also need the Java Development Kit, or JDK, which is the package that includes the javac compiler and the JVM. If you only want to see what a JAVA file contains, though, a plain text editor is all you need, and it is the safe way to inspect one you have been sent.

🔀 Java is Not JavaScript

One point causes endless confusion for newcomers, so it is worth stating plainly, the similar names of Java and JavaScript hide the fact that they are completely separate things.

Despite sharing four letters, Java and JavaScript are different languages that were created separately and are used in different ways. Java is the compiled, class-based language described throughout this guide, whose .java files become bytecode that runs on the JVM, and it is common for Android apps, back-end systems and large applications. JavaScript, by contrast, is a scripting language that grew up in web browsers to make web pages interactive, and its files usually end in .js rather than .java. The shared name is largely a historical marketing choice from the 1990s rather than a sign of any real family tie. So if you have a .java file, you are firmly in the Java world, and the tools and knowledge that apply to JavaScript are a separate topic. Keeping the two apart from the start avoids a great deal of muddle later on.

❓ Frequently Asked Questions

A JAVA file holds Java program code in plain text, the instructions a developer types out in the Java language while building a piece of software. It contains classes, methods and statements laid out according to Java's rules, but it is not something a computer runs directly. Before it can do anything, the code must be compiled, meaning a tool called javac reads the .java file and turns it into a different file of bytecode with a .class extension. That bytecode then runs on the Java Virtual Machine. So a JAVA file is really the human-written starting point of a program, the part a person reads and edits, rather than the finished thing that runs. Because of this, you open a .java file to look at or change its code, not to launch it, and it belongs to software development rather than to everyday documents like images or spreadsheets.

What opening a JAVA file involves comes down to whether your aim is reading the code or building a program from it. Since a JAVA file is plain text, any text editor will open it, so the built-in Notepad app or a Mac's TextEdit will display the contents, while a coding editor like Visual Studio Code or Sublime Text makes it easier to read by colour-coding the syntax. For proper development, most people use an integrated development environment, or IDE, which combines an editor with the tools to compile and run the code, with IntelliJ IDEA, Eclipse and NetBeans being popular choices for Java. To compile and run the code rather than just read it, you also need the Java Development Kit, the package that includes the javac compiler. If you only want to see what is inside a JAVA file, a plain text editor is all you need and is the safe way to inspect one.

A JAVA file becomes a running program in two stages. First it is compiled, where a tool called javac reads your .java source and translates it into a .class file containing bytecode, a compact and portable set of instructions rather than the machine code a processor runs directly. Each class in your file becomes its own .class file, so Hello.java compiles into Hello.class. Second, that bytecode is run by the Java Virtual Machine, or JVM, a program that sits on your operating system and turns the bytecode into the native instructions your computer understands as the program runs, using an approach called just-in-time compilation. The neat part is that the same .class file works on any system that has a JVM, which is what people mean by Java's write once, run anywhere idea, since the code is compiled a single time and each different machine handles the final step itself.

A .java file and a .class file are two stages of the same program. The .java file is the source code, the human-readable text a developer writes in the Java language, full of classes, methods and comments meant for people to understand. The .class file is what the javac compiler produces from that source, and it holds bytecode, a compact machine-oriented form that people are not meant to read and that the Java Virtual Machine can run. In other words the .java is the input a programmer works on and the .class is the compiled output the computer uses. One .java file can produce several .class files, since each class it defines becomes its own .class. When you are writing or studying a program you work with the .java file, and the .class files are generated automatically whenever you compile.

Yes, a JAVA file has to be compiled before it can run, because it is source code rather than a ready-to-run program. On its own the .java file is just text describing what the program should do, and a computer cannot execute that text directly. Running the javac compiler turns it into a .class file of bytecode, and only then can the Java Virtual Machine run the program. This compile-then-run pattern is a core part of how Java works, and it is why you need the Java Development Kit installed, since that package provides the javac compiler. Many development environments hide the steps by compiling automatically when you press a run button, so it can feel like the .java file runs on its own, but behind the scenes the compile step is always happening first.

No, Java and JavaScript are completely different languages despite their similar names. Java is the compiled, class-based language whose .java files become bytecode that runs on the Java Virtual Machine, and it is widely used for Android apps, back-end systems and large applications. JavaScript is a scripting language that was built for web browsers to make web pages interactive, and its files normally end in .js instead of .java. The two were created separately by different groups, and the shared part of the name comes from a marketing decision in the 1990s rather than any real relationship between them. Mixing them up is a very common beginner mistake, so it helps to remember that a .java file belongs to Java, and anything ending in .js belongs to the unrelated world of JavaScript.

You compile and run a JAVA file with two commands from the Java Development Kit, which you need installed first. To compile, you run the javac command followed by the file name, so javac HelloWorld.java turns the source into a bytecode file called HelloWorld.class. To run it, you use the java command followed by the class name, so java HelloWorld starts the Java Virtual Machine and runs the program, and note that you leave off the .class part and give just the name. Together these are the classic compile-then-run steps behind every Java program. Newer versions add a shortcut, since from Java 11 a program that fits in one file can be launched directly with java HelloWorld.java, which compiles and runs in a single step for quick tests. There is also jshell, an interactive tool for trying out Java statements one at a time without creating a file, which is handy when you are learning.

These three fit together like nested boxes, each containing the one before. The JVM, or Java Virtual Machine, is the innermost part, the engine that actually runs bytecode by turning it into native machine instructions and handling tasks like memory management. The JRE, or Java Runtime Environment, wraps the JVM together with the standard code libraries a program depends on, so the JRE is everything needed to run a Java application. The JDK, or Java Development Kit, wraps the JRE with the tools a developer needs, including the javac compiler, the jar packaging tool and a debugger, so the JDK is what you install to write and compile Java. The easy way to remember it is that the JDK contains the JRE, which contains the JVM. If you only want to run Java programs you need the JRE, while if you are writing Java you need the full JDK.

A JAR file, short for Java Archive, is a single file that bundles together many compiled .class files and other resources so a whole Java program can be shipped and run as one unit. It sits further along the chain than a JAVA file, since you write .java source, compile it into .class bytecode, then package those .class files into a .jar for easy distribution. Under the hood a JAR is actually a ZIP file, so archive tools can open it, and it often acts as the runnable form of a Java application, a bit like an .exe on Windows. The jar tool that creates one comes with the Java Development Kit, alongside the javac compiler. So a JAVA file is the human-written starting point and a JAR file is a packaged bundle of the compiled output, two ends of the same journey from source code to a shippable program.

When javac refuses to build a JAVA file, the message it prints usually points straight at the cause, and a few reasons come up again and again. A very common one is a mismatch between the file name and the public class inside it, since Java insists a file be named exactly after its public class, so a public class called Invoice must live in Invoice.java with the same capitalisation. Another frequent cause is a small syntax slip, such as a missing semicolon at the end of a statement or an unmatched curly brace, which stops the compiler in its tracks. You may also see a cannot find symbol error, which usually means a name is misspelled or something has not been imported. Because Java is case-sensitive, mixing up capitalisation in a name counts as a mistake too. The good news is that javac reports the file and line for each problem, so working through the messages one by one is the reliable way to get a file compiling.

You cannot simply convert a .class file back into the original .java source, but you can get close with a tool called a decompiler. Compiling is a one-way trip in the sense that the .class file holds bytecode rather than your original text, and details like comments and some names are not kept. A Java decompiler reads the bytecode and reconstructs readable Java source that behaves the same way, which is often good enough to understand what a class does, though it will not be an exact copy of what the author wrote. This is useful for studying how a compiled library works or recovering the gist of lost code. It is worth remembering that decompiling software you do not own may run into licensing limits, so it is best kept to your own code or cases where you have permission. For your own projects the real answer is always to keep the .java source safe, since that is the file you actually edit.

Yes, Java remains one of the most widely used programming languages in the world, and JAVA files are written and compiled every day. It is a mainstay of large business and back-end systems, where its stability and long track record are valued, and it is the traditional language for Android app development. Its write once, run anywhere design, backed by the Java Virtual Machine, keeps it attractive for software that has to run across many kinds of computer and server. The language has also kept evolving, with regular new versions adding modern features, so it is far from frozen in the past. While newer languages have taken share in some areas, the enormous amount of existing Java code and the size of its community mean the language, and the .java files behind it, will be in active use for a long time to come.

📝 Summary

A JAVA file is the plain-text source code of a program written in the Java programming language, holding classes, methods and statements a developer types out. It is not run directly, since it first has to be compiled, a step where the javac tool reads the .java file and turns it into a .class file containing bytecode, a portable in-between form rather than machine code aimed at one processor. That bytecode then runs on the Java Virtual Machine, or JVM, which translates it into native instructions as the program runs, so the same compiled file works on Windows, macOS or Linux wherever a JVM is present, the idea behind Java's write once, run anywhere slogan. One firm rule is that a JAVA file must be named exactly after the public class it contains, since Java is case-sensitive and allows only one public class per file. To open a JAVA file you can use any text editor to read it, while real development uses an IDE such as IntelliJ IDEA, Eclipse or NetBeans together with the Java Development Kit that supplies the compiler. Finally, Java is not JavaScript, since the two share a name by historical accident but are separate languages used in different ways.