Learning goals
Organize Java source into packages and directories, understand the classpath, compile multi-file projects from the terminal, and recognize how Maven/Gradle layouts map to the same rules—without needing a build tool yet.
Why structure matters
A single-file Hello.java scales poorly. Real projects split code into packages (namespaces) and classes (one public top-level class per file, usually). The filesystem mirrors the package name.
Package naming convention
Packages use reverse domain notation:
package com.example.app;
public class Application {
public static void main(String[] args) {
System.out.println("Started");
}
}
File path (standard layout):
src/
com/
example/
app/
Application.java
Rule: package com.example.app; → directory com/example/app/.
Package names are lowercase. Class names use PascalCase.
Default package (avoid in production)
If you omit package, the class lives in the default package. Fine for tiny experiments; real code always uses named packages to avoid collisions.
Compiling with packages
From the src directory (or project root with -sourcepath):
javac com/example/app/Application.java
Or compile everything:
javac $(find . -name "*.java")
Run:
java com.example.app.Application
Use the fully qualified class name (FQCN): com.example.app.Application.
Classpath
The classpath tells the JVM where to find .class files and JARs.
java -cp src com.example.app.Application
javac -cp lib/some.jar -d out src/com/example/app/Application.java
| Flag | Meaning |
|---|---|
-cp / -classpath |
Search path for classes |
-d out |
Put compiled .class files under out/, preserving package dirs |
Modern style separates source (src/) from output (out/ or target/classes/).
Multi-class example
// src/com/example/app/Greeter.java
package com.example.app;
public class Greeter {
public String greet(String name) {
return "Hello, " + name;
}
}
// src/com/example/app/Application.java
package com.example.app;
public class Application {
public static void main(String[] args) {
Greeter greeter = new Greeter();
System.out.println(greeter.greet("Java"));
}
}
Both files share the same package, so no import is needed. Different packages require import com.other.Util;.
Maven / Gradle layout (preview)
Build tools enforce the same mental model:
my-app/
src/main/java/com/example/app/Application.java
src/test/java/com/example/app/ApplicationTest.java
pom.xml # Maven
build.gradle # Gradle
You will not configure Maven in this course, but interviews assume you recognize src/main/java.
Module path (JDK 9+, awareness)
JPMS modules (module-info.java) add another layer for large apps and libraries. Core learning stays in the classpath world; know that --module-path exists when libraries publish modular JARs.
Common mistakes
- Directory mismatch.
package com.foo;insrc/Foo.java(wrong)—must besrc/com/foo/Foo.java. - Running without FQCN.
java Applicationfails when the class is in a package; usejava com.example.app.Application. - Wrong current directory. Classpath is relative to where you run
java, not where the.javafile lives. - Multiple public classes in one file. Only one
publictop-level class per file, and it must match the filename. - Case sensitivity on Linux CI.
Com.example.Appbreaks on Linux servers even if macOS seemed forgiving.
Practice checkpoint
-
Where does
package org.demo.util;classHelper.javalive?- Answer:
org/demo/util/Helper.java(under source root).
- Answer:
-
Compile and run a two-class project:
Calculatorwithadd(int,int)andMaincalling it.- Answer: Same package,
javacboth files,java Main.
- Answer: Same package,
-
What command puts compiled classes in
out/?- Answer:
javac -d out path/to/Source.java
- Answer:
-
Why use packages?
- Answer: Avoid name collisions, organize code, control visibility (
publicvs package-private).
- Answer: Avoid name collisions, organize code, control visibility (
Interview angles
- “Difference between path and classpath?” —
-sourcepathfor compilation input;-classpathfor locating classes at compile and run time. - “Can two JARs contain the same class?” — Yes; classpath order determines which wins (shadowing)—a common production bug.
- “One public class per file—why?” — Convention and compiler rule for clarity and tooling.