Compilation
Before running the program, you need to compile the Java source code.Compile the program
Use the Java compiler to compile This will generate a
Main.java:Main.class file in the same directory.Quick Start
If you’re already in thesrc directory, you can compile and run in one go:
Troubleshooting
'javac' is not recognized as a command
'javac' is not recognized as a command
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
bindirectory to your system PATH - Verify installation:
javac -version
Error: Could not find or load main class Main
Error: Could not find or load main class Main
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.classexists in the current directory
Error: class Main is public, should be declared in a file named Main.java
Error: class Main is public, should be declared in a file named Main.java
The filename doesn’t match the public class name.Solution:
- Ensure your file is named exactly
Main.java(case-sensitive)
InputMismatchException during execution
InputMismatchException during execution
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:The
-cp (classpath) flag tells Java where to find the compiled .class files.