Running a Single Source File Directly
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
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));
}
}Price: Rs. 1200
Discount: Rs. 120
You pay: Rs. 1080java Discount.javaHow it works
- You give
javathe file name with.java, instead of a class name. - Java compiles the file in memory. No
Discount.classfile is written to your folder. - It runs the first class in the file that has a
mainmethod. - 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 to | Use |
|---|---|
| Try a small idea quickly | java File.java |
| Run a practice exercise or a lesson example | java File.java |
| Build a real application with many packages | javac (or Maven / Gradle) and java |
| Ship a program to other people | Compile 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:
public class Fare {
static int total(int fare, int people) {
return fare * people;
}
}public class Main {
public static void main(String[] args) {
System.out.println(Fare.total(450, 3));
}
}Then run only the file with main:
java Main.java1350Java 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:
#!/usr/bin/env -S java --source 25
void main() {
IO.println("Good morning from a Java script");
}Make it executable and run it:
chmod +x greet
./greetThe 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
- Easy: Write
Square.javathat prints the square of 27, and run it with one command. - Harder: Split it into two files,
Square.javawithmainandMathHelp.javawith astatic int square(int n)method. Run it withjava Square.java. Which Java version do you need?
Key Takeaways
Was this tutorial helpful?