Correct Multiple Packages Error
Copy and paste the following code in the Demo.java
file:
package com.example;
package com.example.test;
public class Demo {
public static void main(String[] args) {
int a = 10, b = 15;
System.out.print("The Sum is: " + (a + b));
}
}
Save the file and run the following command in the terminal:
javac Demo.java
You should see the following error message:
Demo.java:3: error: class, interface, or enum expected
package com.example.test;
^
1 error
This error indicates that there are multiple packages declared in the same file. You can only declare one package per file in Java. To fix the error, remove the extra package declaration:
package com.example;
public class Demo {
public static void main(String[] args) {
int a = 10, b = 15;
System.out.print("The Sum is: " + (a + b));
}
}
Save the file and run the following command again:
javac Demo.java
This time the code will compile without any errors.