Sub topic 9
Resolve Common Setup and Compilation Problems
A Java program can fail at several stages. The JDK might not be installed correctly, the terminal might not find its tools, or the program might contain a typing mistake. The good news is that the error message usually tells you where to start.
This chapter assumes you have tried to install a Java Development Kit, or JDK, and compile HelloWorld.java. The JDK includes javac, the compiler that turns Java source code into a form Java can run. The java command starts the Java runtime, which runs the compiled program.
Start with the first error
When something goes wrong, read the first error message before changing anything. Later messages can be caused by the same first mistake.
Use this table to choose a starting point:
| What you see | Where to look |
|---|---|
| The JDK installer will not run | The installer file and your computer’s requirements |
| `java` or `javac` is not recognized | The installation and command search path |
| `file not found: HelloWorld.java` | Your current folder and the source file’s name |
| A message such as `';' expected` | The Java code near the reported line |
| `Could not find or load main class` | The compiled file, current folder, and run command |
| `UnsupportedClassVersionError` | The compiler and runtime versions |
An error message is a clue, not a judgment. Change one thing at a time, then try the same command again. That makes it easier to see which change solved the problem.
If the installer will not run
Check the exact message from the installer. Common causes include:
- Wrong operating system or processor type. A download for Windows will not install on macOS. A download for an ARM-based computer may not work on an x64 computer, or the other way around. Check your computer’s system information and the download name.
- Operating system too old. Check the JDK provider’s requirements for the version you downloaded.
- Not enough space. Free some disk space and try again.
- Permission denied. A permission is approval to perform an action on the computer. An installer may ask for an administrator account. On a work or school computer, ask the person who manages it rather than trying to bypass the restriction.
- Incomplete download. Download the installer again from the JDK provider’s official site.
After an installation succeeds, open a new terminal window before checking the commands. A terminal is an app where you type commands. An older terminal window may not know about changes made during installation.
If the terminal cannot find Java
In a new terminal, run:
java -version
javac -versionEach command should show a version. The exact version depends on the JDK you installed.
If you see command not found, or Windows says a command “is not recognized,” the terminal cannot find that command in the places it searches.
Check which command is missing
- If both commands are missing, the JDK may not be installed, or its commands may not be available to the terminal.
- If
javaworks butjavacdoes not, you may have a runtime without a compiler, or the terminal may be finding a different Java installation. - If both work, note their versions. They may still come from different installations.
A JDK contains both tools. Check your installed applications or the folder where you installed the JDK. Inside the JDK folder, look for a folder named bin; it contains the JDK’s commands.
Check the command search path
The PATH is a list of folders that the operating system searches for commands. The JDK’s bin folder needs to be available through that list, unless another working Java command is already being used.
On Windows, you can search for Edit environment variables for your account, then edit the Path entry. Add the full path to your JDK’s bin folder. Do not remove the other entries: your computer may need them for other commands. If an older Java entry appears before your intended one, moving the intended entry higher may help.
On macOS and Linux, the way to make a PATH change last depends on your terminal’s shell, the program that reads your commands. Follow the JDK provider’s instructions for your shell. As a temporary test, you can use the form export PATH="/path/to/jdk/bin:$PATH", replacing /path/to/jdk/bin with the actual path. That change applies to the current terminal session; it is not a permanent fix.
After changing PATH, open a new terminal and run the version checks again. On Windows, if a new tab still uses old settings, close the terminal app fully and reopen it.
You might also see instructions about JAVA_HOME. This is a setting that some tools use to locate a JDK. It normally points to the JDK folder, not its bin folder. Setting it alone does not guarantee that the terminal can find java and javac; PATH also matters.
Find the commands being used
If the versions are surprising, look for the command locations.
In Windows Command Prompt, run:
where.exe java
where.exe javacOn macOS or Linux, run:
command -v java
command -v javacWindows may list more than one location. The first matching command is usually the one that runs. On macOS or Linux, a displayed path can lead to a link, which points to another file, or a helper that selects a Java installation. So the path is a useful clue, but it does not always reveal the final JDK by itself.
If the commands point to an unexpected installation, adjust PATH or follow your JDK provider’s instructions for selecting the intended version. Then open a new terminal and check again.
If the compiler cannot find the source file
Suppose you run this command:
javac HelloWorld.javaThe compiler reports error: file not found: HelloWorld.java. Before editing the program, check where the terminal is looking.
The working directory is the folder the terminal currently uses for commands. When you give javac a file name without a folder, it looks in that working directory.
Check your folder
In Windows Command Prompt, run:
cd
dirOn macOS or Linux, run:
pwd
lsThe first command shows your current folder. The second lists items in it. Look for a file named exactly HelloWorld.java.
If you do not see it, find the folder where you saved it and change to that folder. Use the real path on your computer:
- In Windows Command Prompt, a command can look like
cd /d "C:\Users\Ana\Documents\HelloWorld". The/dalso allows the command to change drives. - On macOS or Linux, a command can look like
cd "/Users/ana/Documents/HelloWorld".
Those paths are examples, not paths that will work on every computer. Keep the quotation marks if a folder name contains spaces. After changing folders, list the files again and retry javac HelloWorld.java.
Check the full file name
The file must be named HelloWorld.java. Watch for these mistakes:
HelloWorld.java.txt: the editor added a text-file ending.HelloWorld.txt: the file was not saved with the.javaending.helloworld.java: the letters do not match the intended name.HelloWorld.java.java: the ending was added twice.
The file extension is the ending after the last dot, such as .java or .txt. File browsers sometimes hide extensions. Turn on the option to show file extensions, then check the full name.
Save the file as plain text, not as a formatted document. Plain text contains characters without document formatting such as fonts or page layouts.
A worked diagnosis
Imagine that javac HelloWorld.java reports file not found. You list the current folder and see HelloWorld.java.txt.
The compiler is looking for a different name. Rename or save the file as HelloWorld.java, then list the folder again to confirm. Run javac HelloWorld.java once more.
If the compiler now reports an error on a line of code, that is progress: it found the file and began reading it.
If the compiler reports a code error
A syntax error is a mistake in the form of the code, such as a missing mark. The compiler often reports the file name, a line number, and a short description.
Start at the first reported line. Also check the line just before it: a missing mark there can make the next line look wrong.
Missing semicolon
A semicolon is the ; mark that ends many Java statements. A statement is an instruction in the program.
If you see ';' expected, check that the print statement ends like this:
System.out.println("Hello, World!");
For example, leaving off the final semicolon is enough to cause an error. Add it, save the file, and compile again.
Misspelled names or different capital letters
Java is case-sensitive: uppercase and lowercase letters are different. For example, System is not the same name as system, and println is not the same as printLn.
A message such as cannot find symbol can mean that Java does not recognize a name you typed. Compare the spelling and capital letters with the intended code. Also check for missing quotation marks around the greeting.
Missing or extra punctuation
Java uses matching parentheses, ( ), and braces, { }. A brace is a curly mark used to group code.
Check that each opening mark has its matching closing mark. Also make sure the greeting uses straight double quotation marks, ", not curved quotation marks copied from a formatted document.
If the compiler reports unclosed string literal, it may be missing a closing quotation mark. A string is text inside double quotation marks, such as "Hello, World!".
Class and file names do not match
A class is a named part of a Java program. If the file declares public class HelloWorld, the file must be named HelloWorld.java, with the same capital letters.
An error saying that the class should be declared in a file with a matching name usually means the public class name and file name differ. Fix the name that is wrong, save, and compile again.
If compilation succeeds but the program will not run
When javac HelloWorld.java succeeds, it normally prints no message. It creates a compiled file named HelloWorld.class in the same folder for this example.
If compilation reports errors, fix them and compile again before running. An older .class file might still be present, so running immediately after a failed compilation could run an older version of your program.
Java cannot find the class
For the Hello World example with no package line at the top, run this from the folder containing HelloWorld.class:
java HelloWorldUse the class name, not the file name. Do not add .java or .class to this command.
If you see Could not find or load main class HelloWorld, check that:
- The compilation succeeded and
HelloWorld.classis in the current folder. - You typed
HelloWorldwith the same capital letters. - You are running the command from the folder containing the compiled file.
If those checks pass but the error remains, try:
java -cp . HelloWorldThe -cp option sets the class path, which tells Java where to look for compiled classes. The dot means the current folder. This command is useful if an existing class-path setting is making Java look somewhere else.
These commands assume the source file has no package declaration. If you added one, undo that change for this Hello World exercise and compile again.
Java cannot find the main method
A method is a named set of instructions. Java looks for a method named main as the starting point for this program.
If the message says Main method not found, check that the method starts exactly as follows:
public static void main(String[] args)
Check the lowercase m in main, as well as the spelling and punctuation. Save and compile again before running.
The runtime is too old for the compiled file
An UnsupportedClassVersionError can mean that the compiler made a class file for a newer Java version than the runtime understands.
Run the two version checks again:
java -version
javac -versionIf the compiler is newer than the runtime, select matching tools from the same JDK. Then open a new terminal, check the versions, and compile the source file again before running it. Different version numbers do not always cause a problem, but this error is a strong reason to check them.
Make a small, repeatable test
After any fix, use the same short test:
- Open a new terminal if you changed installation settings or PATH.
- Run
java -versionandjavac -version. - Move to the folder containing
HelloWorld.java. - Compile with
javac HelloWorld.java. - Only if compilation succeeds, run
java HelloWorld.
The expected program output is Hello, World!. If you get a different result, write down the exact command and the first error message. Include your operating system, the two version results, and the folder you were using when you ask for help. Do not share private files or passwords.
Exercise
You run javac HelloWorld.java and see file not found. What should you check before changing the Java code?
Show answer
Check the terminal’s working directory and list its files. Make sure the source file is in that folder and is named exactly HelloWorld.java, not HelloWorld.java.txt.
Exercise
Compilation succeeds, but java HelloWorld reports that it cannot find the main method. What should you inspect?
Show answer
Check that the saved source has public static void main(String[] args) with the correct spelling and capital letters. Save it and compile again before running.