46

In Eclipse, is it possible to change name of the package if it has name (default package) and has classes inside it?

6 Answers 6

84

In the version of Eclipse I'm using (Indigo service release 1), the "Rename" option doesn't come up for the default package (unlike other packages) - but if you select all the classes then right-click and select "Move" that will let you put them in a new package.

(As far as I can tell, the Rename option is for renaming classes without changing their package, but Move certainly works for moving between packages.)

Screenshots of the process:

Original situation

Before

Select all classes and show the Refactor part of the context menu

enter image description here

Select "Move", click on Create Package and type in a name (mypackage in this case)

Move dialog

The final result

After

Alternatively, if you want to move them to an existing package, you could just drag and drop the classes.

For those suggesting Refactor -> Rename on the package, here's a screenshot showing that not working on my machine:

No Rename option

4
  • 1
    I just realized that "for the default package" is key here: for all other packages, there's the "Rename" option, but not the default one.
    – Kjir
    Commented Apr 15, 2012 at 12:11
  • 1
    @Kjir: Hence the "unlike other packages" part of my answer.
    – Jon Skeet
    Commented Apr 15, 2012 at 12:11
  • To be exact: Right Click > Refractor > Move or Rename. Seems to work
    – J.Olufsen
    Commented Apr 15, 2012 at 12:11
  • 1
    @RCola: For the default package? Doesn't work on my build. Will edit my version number into my answer.
    – Jon Skeet
    Commented Apr 15, 2012 at 12:13
9

Yes.

Either just right-click the class and choose Refactor->Rename, or insert the appropriate package foo.bar at the top and use Ctrl-1 to quickfix.

0
1

Create a New package inside the SRC folder and then drag your class file into the New package.So that the default package name getting changed

0
0

Another option is to go into your src folder in the relevant project folder in your workspace and there, create a new folder. Name that folder as whatever package name you want and put all your .java files into that folder. Refresh eclipse, close the .java files and open from the new location.

0

I created a new package under 'Java Package' with the desired name. Then dragged and dropped the source files from default. IDE would ask to refactor; then proceed with it. Default package disappeared and source files updated.

0

Step 1. Create a new package in the same project

Step 2. Click hold and drop the classes from the default package to new package

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.