简体   繁体   English

如何更改 Eclipse 中的 package 名称?

[英]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. 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我想将此 package 重命名为: 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.我试图 select (默认包)--->右键单击--->重构,但现在我只看到一个名为: Infer generic type arguments但不可能更改 ZEFE90A8E604A7C840E88D03A6DB7F6 名称。

Why?为什么? What have I to do in Eclipse to change the name of the package?我要在 Eclipse 中做什么来更改 package 的名称?

First you need to create package: 首先,您需要创建包:

com.myCompany.executabe (src > right click > new > package). com.myCompany.executabe (src>右键单击>新建>包)。

Follow these steps to move the Java files to your new package. 请按照以下步骤将Java文件移动到新包中。

  1. Select the Java files 选择Java文件
  2. Right click 右键点击
  3. Refactor 重构
  4. Move 移动
  5. Select your preferred package 选择您喜欢的包裹

Alt+shift+R键并更改包名称

You can't rename default package since it actually doesn't even exist. 您无法重命名默认包,因为它实际上甚至不存在。 All files in default package are actually in src folder. 默认包中的所有文件实际上都在src文件夹中。

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. 只需创建新包并移动您的课程。

创建一个新包,将类拖放到其中,现在您可以重命名新包

In pack explorer of eclipse> Single Click on the name of Package> click F2 keyboard key >type new name> click finish. 在eclipse的包浏览器中>单击包的名称>单击F2键盘键>键入新名称>单击完成。

Eclipse will do the rest of job. Eclipse将完成其余的工作。 It will rename old package name to new in code files also. 它还会将旧包名重命名为新的代码文件。

Android app package name? Android应用包名称?

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. 只是为了记录,我的案例涉及一个Android应用程序,所以如果它涉及在Android应用程序中重命名包名称,那么你可以直接从AndroidManifest.xml文件,它是Android清单中的第一个字段,你更改你的包命名并更新它。 Then if it shows you errors, so just "clean" your project in the "project" menu. 然后,如果它显示错误,那么只需在“项目”菜单中“清理”您的项目。

“AndroidManifest”文件没有为我更改,我手动更改包名称。

  1. Change the Package Name in Manifest. 在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 然后右键单击src-> refactor - >重命名粘贴您的包名称
  4. select package name and sub package name both 选择包名称和子包名称

  5. press "save" a warning pop ups , press "continue" 按“保存”警告弹出窗口,按“继续”

name changed successfully 名称已成功更改

Create your directory structure in src folder like 在src文件夹中创建目录结构,如

.

means to create subpkg1 folder under pkg1 folder (source folder in eclipse) then place your source code inside and then modify its package declaration. 意味着在pkg1文件夹(eclipse中的源文件夹)下创建subpkg1文件夹,然后将源代码放入其中,然后修改其包声明。

选择默认包中的所有文件, 右键单击 - > 重构... - > 移动..并在对话框中选择一个包或创建一个新包。

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 简短概述: http//www.tutorialspoint.com/eclipse/eclipse_create_java_package.htm

Just create new package and move Classes to created package. 只需创建新包并将Classes移动到创建的包。

(default package) means that is no package. (默认包)表示没有包。

只需转到该类并使用com.myCompany.executabe包替换package语句,此eclipse将为您提供重命名选项,将类移动到新包中并完成所需操作

  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. com.myCompany.executabe是executabe包的路径。 Here com,myCompany,executable are three different package. 这里的com,myCompany,可执行文件是三个不同的包。 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. A.如果您正在使用Eclipse,并且您想要做的就是首先打开要复制的项目(不要忘记打开您需要复制的项目),然后克隆(复制/粘贴)您的Android项目Eclipse左侧的explorer包窗口。当你粘贴时,Eclipse将要求你提供一个新的项目名称。给它一个新的项目名称。由于Eclipse项目名称和目录独立于应用程序名称和包,以下步骤将帮助您了解如何更改包名称。注意:包名称有两种类型。

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). 1.要在Src文件夹中更改软件包的src软件包名称,请按照以下步骤操作:首先需要创建新软件包:(src >>>>右键单击>>>> new >>>>软件包)。

Example of creating package: com.new.name 创建包的示例:com.new.name

Follow these steps to move the Java files from the old copied package in part A to your new package. 按照以下步骤将Java文件从A部分中的旧复制包移动到新包中。

Select the Java files within that old package 选择旧包中的Java文件

Right click that java files 右键单击那些java文件

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: 2.要更改应用程序包名称(主程序包),请执行以下步骤:

First right click your project 首先右键单击您的项目

Then go to "Android tools" 然后转到“Android工具”

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. 它将向您显示应用程序名称将在清单和大多数相关Java文件中更改。 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 Eclipse Kepler/4.3 (and probably most other versions), you can: 在Eclipse Kepler / 4.3(可能还有大多数其他版本)中,您可以:

A: Rename package in all files: 答:在所有文件中重命名包:

  1. Select the project or any of its members in the Project Explorer 在Project Explorer中选择项目或其任何成员
  2. Menu: Search > File... ( ) 菜单:搜索>文件...()
  3. In field "Containing text:" enter the old package name (eg. com.domain.oldpackage) 在“包含文本:”字段中输入旧包名称(例如com.domain.oldpackage)
  4. Replace... () 替换...()
  5. (Eclipse searches the project files for the old package name as a pattern) (Eclipse在项目文件中搜索旧包名称作为模式)
  6. In field "With:" enter the new package name (eg. com.domain.newpackage), OK 在字段“With:”中输入新的包名称(例如com.domain.newpackage),确定
  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: B:重命名src包:

  1. Select the source code package (eg. expand Project > src and select com.domain.oldpackage) in the Project Explorer 在Project Explorer中选择源代码包(例如,展开Project> src并选择com.domain.oldpackage)
  2. Menum: Refactor > Rename... () Menum:重构>重命名...()
  3. In field "New name:" enter the new package name (eg. com.domain.newpackage) 在“新名称:”字段中输入新的包名称(例如,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. 请注意,在每个Replace ...和Rename ...对话框中都保留所有其他默认值,除非您有选择地忽略重命名包名称的实例。

This procedure should automatically clean your project after each of A and B are completed (including the gen/ directory). 完成A和B中的每一个(包括gen /目录)后,此过程应自动清理项目。 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. 并且您应该立即测试项目以确保此过程在进行进一步更改并使其混淆之前有效。

Right Click your Project 右键单击您的项目
Android Tools>>Rename Application Package Just Change Package in there... Android工具>>重命名应用程序包只需更改包... 在此输入图像描述

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 ) 创建了一个具有必需名称的新包- >将我的所有类移动到这个新包中(右键单击上一个包 - > Refractor - > Move

To rename package in eclipse, right click on the package 1.Click on refactor 2.Click on rename 3.Change the name 4.Click OK要在 eclipse 中重命名 package,右键单击 package 1.单击重构 2.单击重命名 3.更改名称

在这里输入图像描述你可以在eclipse中打开类文件,在顶部添加“包XYZ(package_name),然后在显示错误时点击它并选择'移动ABC.java(class_file)到包XYZ(package_name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM