Sub topic 9
Resolve Common Setup and Compilation Problems
A small mistake can stop a Java program from running. The error message usually tells you where to start. Read it before changing your setup or reinstalling anything.
This chapter assumes you have installed a Java Development Kit (JDK) and saved HelloWorld.java. The JDK includes a compiler, which turns Java source code into a class file, and tools to run that file. Source code is the program text you write.
Check the Commands First
In a terminal, the app where you type commands, run:
java -version
javac -versionjava starts the Java runtime, which runs your program. javac starts the compiler.
If a command is not found or is not recognized, the JDK may be missing, or the terminal may not know where to find it. If java works but javac does not, you may have a runtime available without a JDK compiler on your command path.
The command path, often called PATH, is the list of folders your system searches for commands. Check that your installed JDK’s bin folder is on PATH. After changing PATH, close the terminal, open a new one, and run both version checks again.
Check for Different Java Versions
You can have more than one Java version installed. The terminal might find java from one installation and javac from another.
On macOS or Linux, check which commands are found first:
command -v java
command -v javacIn Windows PowerShell, check the matching command locations:
where.exe java
where.exe javacIf a command has no result, it is not available on the current command path. If the results point to different installations, check PATH and make sure the intended JDK’s bin folder comes first. Open a new terminal and check the versions again. Using the same JDK version for both commands is a simple way to avoid confusion.
You might also see UnsupportedClassVersionError when running a class file. This can mean the class was built with a newer Java version than the runtime supports. Use a runtime at least as new as the compiler that made the class file, then try again.
Check the File and Folder
If javac HelloWorld.java reports that it cannot find the file, check the terminal’s current folder. This is the folder where commands look for files unless you give another location.
These commands work in macOS and Linux terminals and in Windows PowerShell:
pwd
lspwd shows the current folder. ls lists its contents. If you do not see HelloWorld.java, move to the folder where you saved it using cd followed by the folder’s path, then list the contents again.
Check the name closely:
- The file must be named
HelloWorld.java, with the same capital letters. - It must not be named
HelloWorld.java.txt. Your file browser may hide the.txtending; turn on file extensions to check. - The name must match
public class HelloWorldinside the file.
Read Compiler Errors
A compiler error means javac could not turn the source file into a class file. The message often names the file and a line number. Look at that line and the line just before it.
For example, suppose your file contains this line without a semicolon:
System.out.println("Hello, World!")
The compiler may report ';' expected. Add the semicolon at the end:
System.out.println("Hello, World!");
Save the file, then compile it again:
javac HelloWorld.javaAnother common message is cannot find symbol. A symbol is a name used in the program. Check the spelling and capital letters near the reported line. For example, System and system are different names in Java.
Fix the first error, save, and compile again. One mistake can cause several later messages. Also, a failed compile can leave an older HelloWorld.class file in the folder, so do not assume the file is up to date just because you can see it.
Check How You Run the Class
After a successful compile, run the class from the folder containing HelloWorld.class:
java -cp . HelloWorldHere, -cp . tells Java to look for classes in the current folder. Use the class name, HelloWorld, not the file name HelloWorld.class. This command assumes your sample has no package line.
If you see Could not find or load main class, check the current folder, the class name’s capital letters, and whether the latest compile succeeded.
If Java says the main method was not found, check that your class contains public static void main(String[] args). The main method is where this program starts. Save and compile the file again after any change.
If the program starts but its output is wrong, read the program lines instead of reinstalling Java. In that case, the tools are already running your code.
Practice
Exercise
java -version works, but javac -version says the command was not found. What should you check?
Show answer
Check that a JDK is installed and that its bin folder is on PATH. Open a new terminal and try both version commands again.
Exercise
The compiler reports ';' expected on your print line. What should you do?
Show answer
Check the print line and the line before it. Add the missing semicolon, save HelloWorld.java, and compile again.