Skip to main content

How to Put a Java Class in a Package

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Packages group related Java classes and give them unique full names, such as com.example.Main. A class joins a package with one package line at the top of its file, and its file sits in folders that match the package's name.

Syntax​

Syntax
package <name>.<name>;

public class <ClassName> {
...
}
PartWhat it does
package com.example;Puts every class in the file into the package com.example. It must be the file's first statement.
FolderThe file lives in com/example/, one folder per part of the name.
Full nameThe class's name everywhere else is com.example.Main.

The Java Language Specification, section 7.4: "A package declaration appears within an ordinary compilation unit to indicate the package to which the compilation unit belongs."

The Example​

The example puts a Main class in the package com.example, compiles it into an out folder and runs it by its full name.

Prerequisites​

  • A JDK, the Java Development Kit, which includes javac, the compiler. This guide uses OpenJDK 25.0.5.1.

Step 1: Write the Class in Its Folder​

Create the folders com/example, and in them a file named Main.java:

com/example/Main.java, which starts with package com.example; and prints Hello from the com.example package.

com/example/Main.java
package com.example;

public class Main {
public static void main(String[] args) {
System.out.println("Hello from the com.example package");
}
}

Step 2: Compile and Run It​

Terminal
javac -d out com/example/Main.java
ls out/com/example
java -cp out com.example.Main
Output
Main.class
Hello from the com.example package

A zsh terminal where javac -d out com/example/Main.java compiles the class, ls out/com/example shows Main.class, and java -cp out com.example.Main prints Hello from the com.example package.

  • javac -d out compiled the file and put Main.class into out/com/example, matching the package.
  • java -cp out com.example.Main told Java to look for classes in out and to run the class by its full name.

The javac and java commands are the same in zsh, bash and PowerShell; in PowerShell, ls lists the folder too, in its own format.

Common Mistakes​

MistakeWhat happensDo this instead
Running the class by its short name, as in java -cp out MainError: Could not find or load main class Main followed by Caused by: java.lang.ClassNotFoundException: MainUse the full name, com.example.Main
Putting the package line below an importerror: class, interface, annotation type, enum, record, method or field expected at the package lineMake package the first statement
Using capital letters or hyphens in package namesHyphens aren't allowed, and capitals break the conventionUse lowercase words separated by dots

Troubleshooting​

SymptomLikely causeFix
Could not find or load main classWrong class name or wrong -cp folderUse the full name and point -cp at the folder that contains com
The class compiled into the wrong folderjavac ran without -dCompile with -d out

FAQ​

Can I still run it with java Main.java?​

Yes. In single-file mode, java com/example/Main.java compiled and ran it, package line and all, and printed the same greeting.

How do I choose a package name?​

By convention, start with a domain you own written backwards, such as com.example, followed by the project's name.

Conclusion​

package com.example; at the top of a file puts its class in that package, the file lives in com/example/, and other code refers to it as com.example.Main. Compile with javac -d and run with java -cp and the full name.

Additional Resources​