I'm trying to setup a project in NetBeans IDE 19 to have two packages in order to test Java protected access modifier.
My setup is the following:
Project: TestAccessModifiersProtected
\- package: com.agheorghe.testaccessmodifiersprotecteda
|- class: TestAccessModifiersProtectedA.java
\- package: com.agheorghe.testaccessmodifiersprotectedb
|- class: TestAccessModifiersProtectedB.java
when trying to run the code in TestAccessModifiersProtectedB.java
class I get the following error:
Error: Could not find or load main class com.agheorghe.testaccessmodifiersprotected.TestAccessModifiersProtected
Caused by: java.lang.ClassNotFoundException: com.agheorghe.testaccessmodifiersprotected.TestAccessModifiersProtected
Command execution failed
I have tried:
com.agheorghe.testaccessmodifiersprotecteda
to com.agheorghe.testaccessmodifiersprotected
and run TestAccessModifiersProtectedB.java
- result: same errorcom.agheorghe.testaccessmodifiersprotectedb
to com.agheorghe.testaccessmodifiersprotected
and run TestAccessModifiersProtectedB.java
- result: same errorTestAccessModifiersProtected
My code is the following:
package com.agheorghe.testaccessmodifiersprotecteda;
public class TestAccessModifiersProtectedA {
protected void display() {
System.out.println("TestAccessModifiersProtectedA");
}
}
package com.agheorghe.testaccessmodifiersprotectedb;
import com.agheorghe.testaccessmodifiersprotecteda.*;
class TestAccessModifiersPotectedB extends TestAccessModifiersProtectedA {
public static void main(String args[]) {
TestAccessModifiersPotectedB obj = new TestAccessModifiersPotectedB();
/* Prints "TestAccessModifiersPotectedA" */
obj.display();
}
}
Any ideas as to why NetBeans fails to run code in TestAccessModifiersProtectedB.java
? Why is it taking the project name (TestAccessModifiersProtected
) literal for my packages + classes?
I would expect to build the path to the class by including the package and the class so as to resolve such cases.
How can I succeed in keeping two packages separated in the same project?
The solution is to name both the package and the class in the way the project is named.
For example, for project TestMyProject
, NetBeans IDE 19 will expect a package: com.mycompany.testmyproject
and a class TestMyProject.java
in that package.