Eclipse - How to “Change package declaration to …”

2019-02-01 21:56发布

I've just imported a large amount of source code into Eclipse and basically the package name is no longer valid (the code has been moved folders). Is there a way to select all the source code in the Package Explorer and hit a hotkey so that all package declarations are correctly resolved for me? I know you can do this with imports by selecting the source and hitting ctl-shift-o, but is also possible for the package declaration?

Update: Refactoring the packages doesn't work as I don't want to change the name or location of the packages, I just need to adjust the package declaration in the Java source code.

7条回答
【Aperson】
2楼-- · 2019-02-01 22:34

If the package declarations are no longer valid, then all such invalid declarations would appear in the Problems view in Eclipse. If you do not see this view, you can open it from Window-> Show View -> Other... -> Problems (under the General tab).

You can filter on problems in the Problems view and correct easily correctable ones, by choosing the Quick fix option in the context menu (available on a right-click). In your case you should see something similar to the screenshot posted below:

Quick fix for incorrect packages

Applying the quick fix options is trivial, as long as you know which one is correct - you would either have to change the package declaration in the class, or the location of the class itself. Unfortunately there is no option to fix the issue across multiple units at one go; you will have to apply the quick fix for every problem.

If you want to filter on problems of only this variety, consider configuring the Problems view to show all errors that have the text content "does not match the expected package" in the error text, as demonstrated in the following screenshots:

Configure Problem Contents

Eclipse Configure Package Problems

查看更多
Explosion°爆炸
3楼-- · 2019-02-01 22:37

I just had the same problem so I wrote a bash script to do it.

function java-package-update { 
  for path in $(find $1 -type f -name "*.java"); do 
    D=$(dirname $path); 
    F=$(basename $path); 
    P=$(echo $D|tr '/' '.'); 
    if egrep -q '^\s*package\s*' $path; then 
      sed -i '' '/^\s*package\s*/s/^\(\s*package\s*\)[^;]*\(;.*\)/\1 '$P'\2/' $path; 
    else 
      echo >&2 "no package in $path";
    fi; 
  done; 
}

The sed command used is the one on OSX. If you're using gnu sed, then you don't need the '' paramater after the -i.

Just paste it in and run it on the directory containing your source. Backup your source first unless you're very brave.

Example:

$ cd /home/me/proj/fred/src
$ ls
com
$ cp -a com com.backup
$ java-package-update com
$ # fingers crossed
$ diff -ru com.backup com

I really should start doing this stuff in a more modern language like perl :)

查看更多
Fickle 薄情
4楼-- · 2019-02-01 22:40

It is an old question, but I ran into the same problem and wrote a very simple bash script. Hope it will help someone.

for i in *.java; do
    sed -i 's/.*package com.example.something;.*/package com.example.something_else;/' $i
done

Basically, the script traverses all java files inside a directory, and each occurrence of package com.example.something; replaces with package com.example.something_else;.

查看更多
爷的心禁止访问
5楼-- · 2019-02-01 22:42
  • ALT+SHIFT+R add underscore at end of package name, hit ENTER twice
  • ALT+SHIFT+R delete the underscore, ENTER twice

Done if there are few packages.

查看更多
Emotional °昔
6楼-- · 2019-02-01 22:45

Right click on the package, select Refactor > Rename. This will update all source files with the new package name.

查看更多
狗以群分
7楼-- · 2019-02-01 22:45

This should do the trick for you.

Import all your files into the default package first and then drag them into the new package, JDT will do the refactoring and change the package declarations across the project.

查看更多
登录 后发表回答