How to change a package name in Eclipse?

JavaEclipseRefactoring

Java 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.

  1. Select the Java files
  2. Right click
  3. Refactor
  4. Move
  5. 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

  1. Select the classes you want to move to a different package name.
  2. Right click - Refactor - Move
  3. 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

  1. Change the Package Name in Manifest.

  2. A warning box will be said to change into workspace ,press "yes"

  3. then right click on src-> refactor -> rename paste your package name

  4. select package name and sub package name both

  5. 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:

  1. Select the project or any of its members in the Project Explorer
  2. Menu: Search > File... ( )
  3. In field "Containing text:" enter the old package name (eg. com.domain.oldpackage)
  4. Replace... ()
  5. (Eclipse searches the project files for the old package name as a pattern)
  6. In field "With:" enter the new package name (eg. com.domain.newpackage), OK
  7. 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:

  1. Select the source code package (eg. expand Project > src and select com.domain.oldpackage) in the Project Explorer
  2. Menum: Refactor > Rename... ()
  3. In field "New name:" enter the new package name (eg. com.domain.newpackage)
  4. 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 ( , then select project, OK) to restore project elements integrity before proceeding. And you should immediately test the project to ensure this procedure worked before making further changes and confounding them.

Solution 19 - Java

Right Click your Project
Android Tools>>Rename Application Package Just Change Package in there... enter image description here

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndreaNobiliView Question on Stackoverflow
Solution 1 - JavaPrabhakaran RamaswamyView Answer on Stackoverflow
Solution 2 - JavaBranislav LazicView Answer on Stackoverflow
Solution 3 - JavaIren PatelView Answer on Stackoverflow
Solution 4 - JavaButtinger XaverView Answer on Stackoverflow
Solution 5 - JavaSajal DuttaView Answer on Stackoverflow
Solution 6 - JavaSyed Rahman MashwaniView Answer on Stackoverflow
Solution 7 - JavaEugenView Answer on Stackoverflow
Solution 8 - JavaEhsan JelodarView Answer on Stackoverflow
Solution 9 - JavaJaoadun NasifView Answer on Stackoverflow
Solution 10 - JavaRitu View Answer on Stackoverflow
Solution 11 - JavaAlex SuoView Answer on Stackoverflow
Solution 12 - Javaisnot2badView Answer on Stackoverflow
Solution 13 - Javacete3View Answer on Stackoverflow
Solution 14 - JavajtomaszkView Answer on Stackoverflow
Solution 15 - JavaPratik ShelarView Answer on Stackoverflow
Solution 16 - JavaMeetView Answer on Stackoverflow
Solution 17 - JavaYosidroidView Answer on Stackoverflow
Solution 18 - JavaMatthewView Answer on Stackoverflow
Solution 19 - JavaPratheeshView Answer on Stackoverflow
Solution 20 - JavaPokhrel RajatView Answer on Stackoverflow