Skip to main content

Compilation

Before running the program, you need to compile the Java source code.
1

Navigate to the source directory

Open your terminal and navigate to the src directory:
cd src
2

Compile the program

Use the Java compiler to compile Main.java:
javac Main.java
This will generate a Main.class file in the same directory.
3

Run the program

Execute the compiled program:
java Main

Quick Start

If you’re already in the src directory, you can compile and run in one go:
javac Main.java && java Main

Troubleshooting

This error means Java is not installed or not in your PATH.Solution:
  • Install the Java Development Kit (JDK) from Oracle or use OpenJDK
  • Add the JDK’s bin directory to your system PATH
  • Verify installation: javac -version
This usually happens when you’re in the wrong directory or the .class file wasn’t generated.Solution:
  • Make sure you’re in the same directory as Main.class
  • Verify the compilation was successful (no errors from javac)
  • Check that Main.class exists in the current directory
The filename doesn’t match the public class name.Solution:
  • Ensure your file is named exactly Main.java (case-sensitive)
This occurs when you enter non-integer values when the program expects integers.Solution:
  • Only enter whole numbers (integers) when prompted
  • Do not enter decimal numbers or text
  • See Input Format for proper input guidelines

Running from Different Directories

If you need to run the program from a parent directory:
# Compile
javac src/Main.java

# Run (note the classpath)
java -cp src Main
The -cp (classpath) flag tells Java where to find the compiled .class files.

Build docs developers (and LLMs) love