Fixing the "Package Does Not Exist" Error
Now that we understand what causes the "package does not exist" error, let's explore ways to fix it. There are several approaches to resolve this issue:
Solution 1: Create the Missing Package and Class
The most straightforward solution is to create the missing package and class. Let's implement this:
mkdir -p ~/project/src/com/example/math
Now, create a new file named Calculator.java in the ~/project/src/com/example/math directory with the following content:
package com.example.math;
public class Calculator {
public static int add(int a, int b) {
return a + b;
}
public static int subtract(int a, int b) {
return a - b;
}
public static int multiply(int a, int b) {
return a * b;
}
public static int divide(int a, int b) {
if (b == 0) {
throw new ArithmeticException("Division by zero");
}
return a / b;
}
}
Now try compiling the files again. First, compile the Calculator.java file, then compile ErrorDemo.java with the proper classpath:
cd ~/project
javac src/com/example/math/Calculator.java
javac -cp src src/com/example/app/ErrorDemo.java
Important: Just like before, we need to use -cp src when compiling ErrorDemo.java so the compiler can find the compiled Calculator.class file.
This time, the compilation should succeed without errors. Now you can run the program:
java -cp src com.example.app.ErrorDemo
You should see the output:
Result: 8
Solution 2: Correct the Import Statement
If you intended to use a different package or class, another solution is to correct the import statement. Let's say we actually wanted to use the StringUtils class we created earlier.
Create a new file named CorrectedDemo.java in the ~/project/src/com/example/app directory with the following content:
package com.example.app;
// Corrected import statement
import com.example.util.StringUtils;
public class CorrectedDemo {
public static void main(String[] args) {
String original = "Hello, Java!";
String reversed = StringUtils.reverse(original);
System.out.println("Original: " + original);
System.out.println("Reversed: " + reversed);
}
}
Compile and run this file:
cd ~/project
javac -cp src src/com/example/app/CorrectedDemo.java
java -cp src com.example.app.CorrectedDemo
You should see the output:
Original: Hello, Java!
Reversed: !avaJ ,olleH
Solution 3: Use Fully Qualified Class Names
If you want to avoid import statements altogether, you can use fully qualified class names:
Create a new file named FullyQualifiedDemo.java in the ~/project/src/com/example/app directory with the following content:
package com.example.app;
// No import statement needed
public class FullyQualifiedDemo {
public static void main(String[] args) {
String original = "Hello, Java!";
String reversed = com.example.util.StringUtils.reverse(original);
System.out.println("Original: " + original);
System.out.println("Reversed: " + reversed);
}
}
Compile and run this file:
cd ~/project
javac -cp src src/com/example/app/FullyQualifiedDemo.java
java -cp src com.example.app.FullyQualifiedDemo
You should see the same output as before.