Java 如何在 Eclipse 中更改包名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18977407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change a package name in Eclipse?
提问by AndreaNobili
In Eclipse I have a simple Java project that contains a package named (default package)and inside this package I have a class.
在 Eclipse 中,我有一个简单的 Java 项目,其中包含一个名为(默认包)的包,在这个包中我有一个类。
I want to rename this package into something like: com.myCompany.executable
我想将此包重命名为:com.myCompany.executable
I tried to select the (default package) ---> right click ---> refactorbut now I see only the single voice named: Infer generic type argumentsbut not the possibility to change the package name.
我试图选择(默认包)---> 右键单击 ---> 重构,但现在我只看到名为:推断泛型类型参数的单个声音,但不能更改包名称。
Why? What have I to do in Eclipse to change the name of the package?
为什么?我必须在 Eclipse 中做什么才能更改包的名称?
采纳答案by Prabhakaran Ramaswamy
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 文件移动到您的新包中。
- Select the Java files
- Right click
- Refactor
- Move
- Select your preferred package
- 选择 Java 文件
- 右键点击
- 重构
- 移动
- 选择您喜欢的套餐
回答by Alex Suo
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 文件夹,然后将源代码放入其中,然后修改其包声明。
回答by isnot2bad
Select all files in the default package, right click-> Refactor...-> Move..and select a package in the dialog or create a new one.
选择默认包中的所有文件,右键单击-> Refactor...-> Move..并在对话框中选择一个包或创建一个新包。
回答by cete3
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
回答by jtomaszk
Just create new package and move Classes to created package.
只需创建新包并将类移动到创建的包。
(default package)means that is no package.
(默认包)表示没有包。
回答by Iren Patel
Press Alt+shift+R
key and change package name
按Alt+shift+R
键并更改包名称
回答by Branislav Lazic
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.
只需创建新包并在其中移动您的类。
回答by Pratik Shelar
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
只需转到该类并将 package 语句替换为 package com.myCompany.executabe,此 Eclipse 将为您提供重命名将类移动到新包的选项,并将执行必要的操作
回答by Buttinger Xaver
create a new package, drag and drop the class into it and now you are able to rename the new package
创建一个新包,将类拖放到其中,现在您可以重命名新包
回答by Sajal Dutta
- Select the classes you want to move to a different package name.
- Right click - Refactor - Move
- Create Package
- 选择要移动到不同包名称的类。
- 右键-重构-移动
- 创建包