[java] 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?

This question is related to java eclipse refactoring

The answer is


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


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.


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.


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)


Just create new package and move Classes to created package.

(default package) means that is no package.


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


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.


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.


Press Alt+shift+R key and change package name


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.


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


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.


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


  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)


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

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


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


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to eclipse

How do I get the command-line for an Eclipse run configuration? My eclipse won't open, i download the bundle pack it keeps saying error log strange error in my Animation Drawable How to uninstall Eclipse? How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Class has been compiled by a more recent version of the Java Environment Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory How to downgrade Java from 9 to 8 on a MACOS. Eclipse is not running with Java 9 "The POM for ... is missing, no dependency information available" even though it exists in Maven Repository The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. on deploying to tomcat

Examples related to refactoring

Find and replace Android studio How to change a package name in Eclipse? How do I deal with "signed/unsigned mismatch" warnings (C4018)? How to use IntelliJ IDEA to find all unused code? What are some alternatives to ReSharper? Replace Multiple String Elements in C# Python: avoiding pylint warnings about too many arguments Detecting superfluous #includes in C/C++? How to simplify a null-safe compareTo() implementation? Find unused code