Flatten package

I have a package com.foo.bar.  I want to move all classes and subpackages of com.foo.bar into com.foo.  How do I do that?  I've tried doing the following:

  • Refactor -> Move -> Move package com.foo.bar to another package
    • This does nothing as it will create bar back under foo, rather than moving the contents of bar directly into foo.
  • Refactor -> Move -> Move everything from .... to another directory
    • This deletes my classes under bar, but they're not copied under foo.

I think the 2nd option is what I'm supposed to use, but it appears broken.  Anybody else know how to do this?



Hey i was just messing around to see if i could do what you were asking,

Here is what i did

com.foo.bar --> "Refactor" --> "Move"  --> "Move Package 'com.foo.bar' to another package

Enter for:: To Package -> "com"
uncheck "move to another source folder"

delete foo from the com folder

com.bar -> Refactor --> Rename --"foo"

Maybe that helps? I dunno it seems round about to me.


Thanks for the reply.  I forgot to mention that there are already classes in the target package (foo), so I would think your solution might not work.  I believe this this may be a bug in refactoring.  I would think the second option should copy the existing contents of bar into the parent directory.


Hey Adam,

I agree i think this isnt working as good as it could, but I got another idea for what you could try to do

This process is easiet if you do not have "flatter packages" selected, just compact empty middle packages.
you start with

com.foo.(whatever classes/packages are in here)
com.bar.(whatever classes/packages are in here)

Select everything in your com.foo folder. Any packages, classes whatever and drag them into com.bar
you will get the move dialog, hit ok
(everything will turn red, but thats ok...)

delete the foo package (it should be empty with everything now in the bar package)

select com.bar --> refactor --> rename --> "foo"

Everything should go back to normal.

I know this isnt an ideal, and "one click" solution but i think it will work.



Please sign in to leave a comment.