How to change a package name in Eclipse?
JavaEclipseRefactoringJava Problem Overview
In Eclipse I have a simple Java project that contains a package named (default package) and inside this package I have a class.
I want to rename this package into something like: com.myCompany.executable
I tried to select the (default package) ---> right click ---> refactor but now I see only the single voice named: Infer generic type arguments but not the possibility to change the package name.
Why? What have I to do in Eclipse to change the name of the package?
Java Solutions
Solution 1 - Java
First you need to create package:
com.myCompany.executabe
(src > right click > new > package).
Follow these steps to move the Java files to your new package.
- Select the Java files
- Right click
- Refactor
- Move
- Select your preferred package
Solution 2 - Java
You can't rename default package since it actually doesn't even exist. All files in default package are actually in src
folder.
src--
|
MyClass1.java <==== These files are in default package
MyClass2.java
|
org
|
mypackage
|
MyClass3.java <=== class in org.mypackage package
Just create new package and move your classes within.
Solution 3 - Java
Press Alt+shift+R
key and change package name
Solution 4 - Java
create a new package, drag and drop the class into it and now you are able to rename the new package
Solution 5 - Java
- Select the classes you want to move to a different package name.
- Right click - Refactor - Move
- Create Package
Solution 6 - Java
In pack explorer of eclipse> Single Click on the name of Package> click F2 keyboard key >type new name> click finish.
Eclipse will do the rest of job. It will rename old package name to new in code files also.
Solution 7 - Java
Android app package name?
Just for the record, my case concerned an android app so if it concerns renaming the package name in an android application, then you can do it directly from the AndroidManifest.xml file, it's the first field in the android manifest, you change your package name and update it. Then if it shows you errors, so just "clean" your project in the "project" menu.
Solution 8 - Java
"AndroidManifest" file not changed for me and i change package name manually.
Solution 9 - Java
-
Change the Package Name in Manifest.
-
A warning box will be said to change into workspace ,press "yes"
-
then right click on src-> refactor -> rename paste your package name
-
select package name and sub package name both
-
press "save" a warning pop ups , press "continue"
name changed successfully
Solution 10 - Java
I have tried this and quite Simple process:
Created a new Package with required name--> Moved all my classes to this new package (Right click on previous package --> Refractor--> Move)
Solution 11 - Java
Create your directory structure in src folder like
means to create subpkg1 folder under pkg1 folder (source folder in eclipse) then place your source code inside and then modify its package declaration.
Solution 12 - Java
Select all files in the default package, right click -> Refactor... -> Move.. and select a package in the dialog or create a new one.
Solution 13 - Java
Try creating a new package and then move your files in there.
A short overview : http://www.tutorialspoint.com/eclipse/eclipse_create_java_package.htm
Solution 14 - Java
Just create new package and move Classes to created package.
(default package) means that is no package.
Solution 15 - Java
Just go to the class and replace the package statement with package com.myCompany.executabe after this eclipse will give you options to rename move the class to the new package and will do the needful
Solution 16 - Java
com.myCompany.executabe is the path of executabe package. Here com,myCompany,executable are three different package. You have to manually rename individual package name you want to change.
Solution 17 - Java
HOW TO COPY AND PASTE ANDROID PROJECT
A. If you are using Eclipse and all you want to do is first open the project that you want to copy(DON"T FORGET TO OPEN THE PROJECT THAT YOU NEED TO COPY), then clone(copy/paste) your Android project within the explorer package window on the left side of Eclipse. Eclipse will ask you for a new project name when you paste. Give it a new project name. Since Eclipse project name and directory are independent of the application name and package, the following steps will help you on how to change package names. Note: there are two types of package names.
1.To change src package names of packages within Src folder follow the following steps: First you need to create new package: (src >>>> right click >>>> new >>>> package).
Example of creating package: com.new.name
Follow these steps to move the Java files from the old copied package in part A to your new package.
Select the Java files within that old package
Right click that java files
select "Refactor" option
select "Move" option
Select your preferred package from the list of packages in a dialogue window. Most probably you need to select the new one you just created and hit "OK"
2.To change Application package name(main package), follow the following steps:
First right click your project
Then go to "Android tools"
Then select "Rename Application package"
Enter a new name in a dialogue window , and hit OK.
Then It will show you in which part of your project the Application name will be changed.
It will show you that the Application name will be changed in manifest, and in most relevant Java files. Hit "OK"
YOU DONE in this part, but make sure you rebuild your project to take effect.
To rebuild your project, go to ""project" >>> "Clean" >>>> select a Project from a projects
list, and hit "OK". Finally, you can run your new project.
Solution 18 - Java
In Eclipse Kepler/4.3 (and probably most other versions), you can:
A: Rename package in all files:
- Select the project or any of its members in the Project Explorer
- Menu: Search > File... (
) - In field "Containing text:" enter the old package name (eg. com.domain.oldpackage)
- Replace... (
) - (Eclipse searches the project files for the old package name as a pattern)
- In field "With:" enter the new package name (eg. com.domain.newpackage), OK
- Proceed to confirm unless previews show some kind of conflict (which would probably mean that there's something else to be cleaned up before renaming the package)
B: Rename src package:
- Select the source code package (eg. expand Project > src and select com.domain.oldpackage) in the Project Explorer
- Menum: Refactor > Rename... (
) - In field "New name:" enter the new package name (eg. com.domain.newpackage)
- Proceed to confirm unless previews show some kind of conflict (which would probably mean that there's something else to be cleaned up before renaming the package)
Note that in each of the Replace... and Rename... dialogs leave all other defaults, unless you're selectively omitting renaming instances of the package name.
This procedure should automatically clean your project after each of A and B are completed (including the gen/ directory). But if you see any errors you should manually clean and rebuild (
Solution 19 - Java
Solution 20 - Java
enter image description hereYou can open the class file in eclipse and at the top add "package XYZ(package_name), then while it shows error click to it and select ' move ABC.java(class_file) to package XYZ(package_name)