Create your own package
18 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)One line at the top of a file puts its class in a package: package com.college.students;. The file must also sit in the matching folder, com/college/students.
The package statement
Your Welcome class should belong to the package com.college.students. The package statement, the word package, the package name and a semicolon, does that. It must be the first line of code in the file.
Example 1 · A class in a package
package com.college.students;
public class Welcome { public static void main(String[] args) { System.out.println("Welcome to the college app"); System.out.println("This class is in com.college.students"); }}Welcome to the college app This class is in com.college.students
What this code does
- Line 1 puts
Welcomeincom.college.students, so its full name iscom.college.students.Welcome. - Only comments may come above the package line, and a file has at most one.
A file with no package line is in the default package, a package with no name. All your programs so far were there. That is fine for practice; real apps use named packages.
Find the bug
Fix the package line
Select the line that is wrong.
This file should put Topper in the package com.college.students.
Round 1 of 4Folders follow the package name
Where do you save Welcome.java? Each part of the package name is one folder, so the file goes in src/com/college/students/. The command javac -d out then builds the same folders for the class files.
Run by the full name
After compiling, java Welcome does not work any more. A class in a package is run by its full name, and -cp out (the class path) tells Java which folder holds the class files.
Example 2 · Compile and run from the terminal
javac -d out src/com/college/students/Welcome.javajava -cp out com.college.students.WelcomeWelcome to the college app This class is in com.college.students
What this code does
javac -d outwritesout/com/college/students/Welcome.class, as in the picture.java -cp out+ the full name runs it. Run both commands in the folder that holdssrc.
Example 3 · Running with the short name fails
java -cp out WelcomeError: Could not find or load main class Welcome Caused by: java.lang.ClassNotFoundException: Welcome
What this code does
- Java looks for a class named just
Welcomewith no package, and there is no such class inout. - Fix: run
java -cp out com.college.students.Welcome.
For several files, list each one after javac -d out, separated by spaces. Then run the class that has main.
Two classes in one package
StudentApp needs the grade that a method of MarksUtil works out. Classes in the same package use each other directly, with no import and no full name.
Example 4 · Two classes in one package
// File: src/com/college/students/MarksUtil.javapackage com.college.students;
public class MarksUtil { public static String grade(int marks) { if (marks >= 75) { return "A"; } else if (marks >= 50) { return "B"; } return "C"; }}
// File: src/com/college/students/StudentApp.javapackage com.college.students;
public class StudentApp { public static void main(String[] args) { System.out.println("Asha: " + MarksUtil.grade(86)); System.out.println("Ravi: " + MarksUtil.grade(58)); }}Asha: A Ravi: B
What this code does
- Both files start with
package com.college.students;, soStudentAppcallsMarksUtil.grade(86)with no import. - Compile both:
javac -d out src/com/college/students/MarksUtil.java src/com/college/students/StudentApp.java. - Run the class that has
main:java -cp out com.college.students.StudentApp.
Create a package in an IDE
An IDE makes the folders and the package line for you. You still run the class with the Run button, as before.
- IntelliJ IDEA: right-click
src→ New → Package, typecom.college.students. Then right-click the package → New → Java Class. - Eclipse: right-click
src→ New → Package, then right-click the package → New → Class. - VS Code: create the folders
com/college/studentsinsidesrc, then add the file. Check that the first line is the package line; add it if it is missing.
Next step
Check your understanding
8 quick questions