How to change a package name in Eclipse?

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?

217545 次浏览

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.

Select all files in the default package, right click -> Refactor... -> Move.. and select a package in the dialog or create a new one.

尝试创建一个新的包,然后将文件移动到其中。

A short overview : http://www.tutorialspoint.com/eclipse/eclipse_create_java_package.htm

Just create new package and move Classes to created package.

(default package) means that is no package.

Press Alt+shift+R key and change package name

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.

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

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

create a new package, drag and drop the class into it and now you are able to rename the new package

  1. Select the classes you want to move to a different package name.
  2. Right click - Refactor - Move
  3. Create Package

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.

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.

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.

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.

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.

"AndroidManifest" file not changed for me and i change package name manually.

  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

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)

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

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)

To rename package in eclipse:

  1. Right click on the package
  2. Click on refactor
  3. Click on rename
  4. Change the name
  5. Click OK