Java Organise Names with Packages
A package groups related types and gives them a qualified name. It helps avoid name collisions and establishes a package-level access boundary.
Understand the idea
A package declaration comes before imports and type declarations. The compiler’s output root should correspond to the start of the package hierarchy, rather than an arbitrary folder deep inside it. Imports make type names convenient in source; they do not download dependencies or copy classes into a project. Use your own project namespace instead of placing application classes inside java.* packages.
Run it locally
Use a local JDK 21 or newer. Save the complete example as Main.java in a separate lab folder, then compile and run it with the commands below. Java examples do not run in Bhylu’s browser playground. The -ea option enables assertion checks used by some lessons.
javac --release 21 -encoding UTF-8 -d out Main.java
java -ea -cp out workshop.MainA worked example
package workshop;
public class Main {
public static void main(String[] args) {
System.out.println(Main.class.getPackageName());
}
}
Expected output
workshopSave this source as Main.java, compile with javac --release 21 -d out Main.java, and launch it with java -cp out workshop.Main. The compiler writes the class beneath out/workshop. The launcher uses the qualified name, while -cp identifies the classpath root. A missing package directory in a launch path often indicates the wrong root rather than missing source.
Check the official Java documentation for detailed contracts and supported options.