Google+

215. Renaming the Packages in Hiearchy







Pre-requisites -



As we all know that we can rename the packages which are not in hierarchy without any problems. But when you want to rename a package which has sub-packages under it, it is not possible to do it from Eclipse IDE. Instead we have to go to the Project location on your computer, rename the folder with package name outside Eclipse IDE and refresh the Project in Eclipse IDE.

Lets implement this on Eclipse IDE -

1. Launch Eclipse, Create a Java Project 'Project 42' as shown below -


Now lets create a package under Project 42 and rename it by following the below steps.

2. Create a Package 'packageA' as shown below -


3. Now Right click on the Package and select 'Refactor' -> 'Rename' option as shown below -


4. Observe that 'Rename Package' dialog is displayed with the 'packageA' name as shown below -


5. Now change the package name in the 'New Name' field from 'packageA' to 'packageX' and click on 'OK' button as shown below -


6. Observe that the package name got changed from 'packageA' to 'packageX' as shown below -



Now lets create sub-packages under the package 'packageX' and try to rename it by following the below steps.

7. Create two packages package1 & package2 under 'packageX' as sub-packages to it as shown below -


In Eclipse IDE, rename the package 'packageX' , which is having two sub-packages 'package1' and 'package2' by following the below steps.

8. Right click on the 'packageX' and select 'Refactor' -> 'Rename' option as shown below -



9. Observe that 'Rename Pakcage' dialog is displayed with 'packageX' name as shown below -


10. Change the package name displayed in the 'New Name' field from 'packageX' to 'packageG' and click on 'OK' button as shown below -


11. Observe that a new package 'packageG' got added instead of renaming the 'packageX' to 'packageG' as shown below -


Hence we are not able to rename the package having the sub-packages using Eclipse IDE. So renaming the packages having the sub-packages from Eclipse IDE is not possible.

Now lets find out the project location and change the package folder name to change it by following the below steps.

12. Right click on the Project and select 'Properties' option as shown below -


13. In the 'Properties for Project 42' dialog, select 'Resources' option from the left and view the location of the project on your computer as shown below -


14. Go to the Project location on your computer and open the 'src' folder to view the package folder 'packageX' inside it as shown below -


15. Rename the 'packageX' folder manually to 'packageM' as shown below -


16. Observe that the package name is still not changed in the Eclipse IDE as shown below -


Package name wont be changed in Eclipse IDE until you refresh the Project. Refresh the project by following the below steps.

17. Right click on the Project in Eclipse IDE and select 'Refresh' option as shown below -


18. Observe that the package name got changed from 'packageX' to 'packageM' in Eclipse IDE as shown below -


Hence renaming the package having the sub-packages from Eclipse IDE is not possible. In order to rename, we have to change the name of the package folder from the Project location in our computer and refresh the Project in Eclipse IDE to see the changes.

Can we rename any package having Class files under it from Eclipse IDE ?

Yes, we can rename the package name in Eclipse which has any Class files under it. But we cannot rename the package name in Eclipse IDE if it has any sub-packages under it.

Lets implement this on Eclipse IDE -

1. In Eclipse IDE, create two Class files 'ClassOne' & 'ClassTwo' under the package 'packageG' in the existing Java Project 'Project 42' as shown below -


2. Right click on the 'packageG' which is having two Classes 'ClassOne' & 'ClassTwo' and select 'Refactor' -> 'Rename' option as shown below -


3. Observe the 'Rename Package' dialog is displayed with 'packageG' name as shown below -


4. Change the name of the package in 'New Name' field from 'packageG' to 'packageS' and click on 'OK' button as shown below -


5. Observe that the package name got changed from packageG to packageS without any problems in Eclipse IDE as shown below -


Hence we can renaming the packages having Class under it from Eclipse IDE without any problems.





Please comment below to feedback or ask questions.

Interface Implemented methods must be declared as public will be explained in the next post.





No comments: