A traditional Java workflow has two distinct steps: javac compiles a source file into bytecode, and java launches a class containing a suitable main method. Compilation errors and runtime errors occur at different boundaries and require different checks. This lesson follows a small program from filename to class file to output, then explains classpath roots, arguments, and Java's source-file launcher. The commands assume you are in the directory containing the example unless a command explicitly states otherwise.
Match the filename to a public class
A public top-level class named ParcelLabel belongs in ParcelLabel.java, with matching capitalization. This naming rule helps the compiler locate declarations predictably. The compiler reads the source, checks syntax and types, and normally writes a .class file for each compiled class. Inner classes can create additional class files. A successful compilation does not imply the program's business logic is correct; it establishes that the source meets the checked language and type requirements.
Understand the main entry point
The familiar entry point is public static void main(String[] args). public allows the launcher to access it, static avoids requiring a constructed instance, and void means it returns no result to the caller. args receives command-line strings. Java 17 programs in this course use that traditional signature. Newer Java releases may support additional source and entry-point forms, but copying those forms into a Java 17 project can fail.
Launch a class rather than a filename
After compilation, java ParcelLabel asks the launcher to find the class with that binary name. java ParcelLabel.class is not the normal class launch command. The classpath tells the runtime where class roots and JAR files are located. Using -cp . explicitly selects the current directory for an unpackaged class. If a named package is present, launch with the fully qualified name and point the classpath at the directory above the package hierarchy.
Distinguish compile and runtime failures
An unresolved variable or incompatible assignment is a compile-time error, so no new successful build is produced. A division by zero reached during execution is a runtime exception. Could not find or load main class usually points to launch naming or classpath, not the arithmetic inside main. An old class file can remain after a failed compile; avoid accidentally running that stale output and assuming the edited source worked.
Pass and validate arguments
Values following the main class name become args entries. The shell handles quoting before Java receives them, so quote a multiword argument as one value when necessary. Check args.length before reading a position. Convert numeric arguments using parsing methods and handle invalid text according to the application's contract. The launcher does not validate that an argument called quantity is actually a positive integer; that is your program's responsibility.
Use the source-file launcher deliberately
Since Java 11, java FileName.java can compile and run a suitable source file for quick use. This is different from launching a .class filename, and it does not replace a project's build system or dependency configuration. The two-step javac workflow makes compilation output easier to inspect while learning. Use a separate output directory with javac -d when managing several files, keeping generated class files out of the source directory.
Practice tasks and checks
Compile ParcelLabel into out and launch it using that explicit classpath. Change the message, run the existing class once, and observe that it still contains the old message until compilation succeeds again. Then recompile and confirm the new output. This controlled experiment explains stale bytecode without introducing a logic bug. Launch ArgumentGreeting with no arguments, one simple name, and a quoted two-word name; inspect the difference between one argument and two arguments by adding a print of args.length. Finally, deliberately misspell the class name in the launch command and classify the resulting failure as class lookup rather than a syntax error. Restore the correct command and keep a small record of source, compiled output, and runtime search root.
Java Summary
- Compilation and launching are separate steps with separate failures.
- Launch a binary class name without a .class extension.
- Validate arguments before indexing or parsing them.
- Continue with classpaths and packages when these examples are clear.
Java Examples
Each example shows the code, the output it produces and what each part does.
Compile and launch the same class
Save this program as ParcelLabel.java. Compile it with javac ParcelLabel.java and run it with java ParcelLabel.
Expected output:
Save the file with the public class's exact name. The compiler verifies the main method and produces ParcelLabel.class. Launching ParcelLabel invokes main, whose print statement emits one line. Nothing about the filesystem's name decides the message; it comes from the string literal in your source. If you edit that literal, compile again before launching the class to avoid running stale bytecode.
public class ParcelLabel {
public static void main(String[] args) {
System.out.println("Parcel accepted");
}
}Parcel acceptedKeep bytecode in a separate directory
Use the following shell commands or configuration.
What to expect: Parcel accepted
The compiler creates the output directory structure it needs under out and targets the Java 17 platform. The launcher then searches out rather than the source directory. These commands express separate compile and runtime locations clearly. Run them from the directory containing ParcelLabel.java. If another folder also contains an older class with that name, -cp out helps avoid selecting it accidentally.
javac --release 17 -d out ParcelLabel.java
java -cp out ParcelLabelRead a command-line argument safely
Save this program as ArgumentGreeting.java. Compile it with javac ArgumentGreeting.java and run it with java ArgumentGreeting.
Expected output:
Launching without arguments selects Guest because the array has length zero. Launching java ArgumentGreeting Mina instead supplies Mina as the first string and prints Welcome, Mina. Checking the length prevents ArrayIndexOutOfBoundsException on an empty argument array. The default is an explicit application decision; Java does not insert Guest automatically. More complicated programs should document required arguments and reject invalid forms with a useful usage message.
public class ArgumentGreeting {
public static void main(String[] args) {
String name = args.length == 0 ? "Guest" : args[0];
System.out.println("Welcome, " + name);
}
}Welcome, GuestJava FAQ
Why should I omit .class in the launch command?
The class launcher resolves a binary class name through the classpath. A class-file extension is part of a filesystem filename, not that binary name. Source-file launching with a .java filename is a different supported mode.
Can I run after a failed compilation?
An older class file may still exist, but it will not represent your failed edit. Treat a nonzero compiler result as a failed build and correct it before claiming the updated source ran successfully.
What does "Could not find or load main class" mean?
The launcher couldn't find a class with that name on the classpath. Common causes are adding .class to the command, running from the wrong folder, a capitalisation mismatch, or leaving out the package name for a class that declares one, as in java tools.ParcelLabel.
Why does javac say a public class must be declared in a file with its name?
A public top-level class must live in a file with exactly the same name, including capital letters. public class ParcelLabel in Label.java fails with "class ParcelLabel is public, should be declared in a file named ParcelLabel.java". Rename the file or the class so they match.
What is the difference between java Main and java Main.java?
java Main runs an already compiled Main.class found on the classpath. java Main.java uses source-file mode: it compiles the file in memory and runs it straight away without writing a .class file. Source-file mode is handy for small programs and scripts; larger projects use javac or a build tool.
Java Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
Which command targets Java 17 while compiling?
What does -cp out select?
Java Quiz
2 questions to check what you learned.
What protects args[0] when no arguments were supplied?
- Checking args.length
- Adding a package statement
- Making the variable static
What does java ParcelLabel.java use on a supported JDK?
- Source-file launching
- A direct class-file launch
- The jar tool