Running a Single Source File Directly

Naveen AnamaneniUpdated · 5 min read

For years, even a two-line Java experiment meant two commands and a .class file left behind. Python users just typed python file.py. Since Java 11, Java can do the same: java Hello.java compiles and runs in one step. It's the quickest way to try an idea, and it's how many examples in this course are checked.

One Command Instead of Two

Two steps vs one step
Discount.java▶ Try it
public class Discount {
    public static void main(String[] args) {
        int price = 1200;
        int discount = price * 10 / 100;
        System.out.println("Price:    Rs. " + price);
        System.out.println("Discount: Rs. " + discount);
        System.out.println("You pay:  Rs. " + (price - discount));
    }
}
Output
Price:    Rs. 1200
Discount: Rs. 120
You pay:  Rs. 1080
Terminal
java Discount.java

How it works

  1. You give java the file name with .java, instead of a class name.
  2. Java compiles the file in memory. No Discount.class file is written to your folder.
  3. It runs the first class in the file that has a main method.
  4. Line 4 works out 10% of the price. Line 7 needs brackets around price - discount, so the subtraction happens before the number is joined to the text.

Try it yourself: remove the brackets on line 7 and run it again. The compiler now refuses: Java joins the text and price first, and you can't subtract a number from text.

When to Use Which

You want toUse
Try a small idea quicklyjava File.java
Run a practice exercise or a lesson examplejava File.java
Build a real application with many packagesjavac (or Maven / Gradle) and java
Ship a program to other peopleCompile it and package it in a .jar (covered later in the course)

Compile errors still appear exactly as they do with javac, with the file name and line number. The only difference is that Java runs the program straight away when there are none.

Using Other Files in the Same Folder (Java 22+)

From Java 22, the launcher can use classes from other .java files in the same folder. Put Main.java and Fare.java side by side:

Fare.java
public class Fare {
    static int total(int fare, int people) {
        return fare * people;
    }
}
Main.java
public class Main {
    public static void main(String[] args) {
        System.out.println(Fare.total(450, 3));
    }
}

Then run only the file with main:

Terminal
java Main.java
Output
1350

Java compiles Main.java, notices that it needs Fare, finds Fare.java in the same folder and compiles that too. On Java 11 to 21 this fails, because only the one file you named is compiled.

Java Files as Scripts (macOS and Linux)

On macOS and Linux you can run a Java file like a shell script. Save this as a file named greet, without .java on the end:

greet
#!/usr/bin/env -S java --source 25
void main() {
    IO.println("Good morning from a Java script");
}

Make it executable and run it:

Terminal
chmod +x greet
./greet

The first line, called a shebang, tells the system to run the file with java --source 25. The file must not end in .java, or Java treats the first line as a syntax error. The void main() form is explained in the next lesson.

Common Mistakes

Expecting a .class file afterwards. The source launcher compiles in memory only. If another program needs the .class file, use javac.

Having an old Discount.class in the folder. java Discount runs the old compiled file; java Discount.java always uses the current source. Know which one you typed.

Using an older JDK. Single-file running needs Java 11 or newer, and using other source files needs Java 22 or newer. Check with java -version.

Practice

  1. Easy: Write Square.java that prints the square of 27, and run it with one command.
  2. Harder: Split it into two files, Square.java with main and MathHelp.java with a static int square(int n) method. Run it with java Square.java. Which Java version do you need?

Quick check: Single-file programs

4 questions
1What does java Discount.java do?
2After java Discount.java, is there a Discount.class file in the folder?
3From which version can the launcher use classes from other .java files in the same folder?
4A script file starts with #!/usr/bin/env -S java --source 25. What must its name NOT end with?

Key Takeaways

Was this tutorial helpful?

Lesson 16 of 482