Java 如何从 Eclipse 导入包?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2681161/
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 import a package from Eclipse?
提问by Roman
In one of my directories I have all .java files which belong to one package ("game"). Now I want to create one .java file which does not belong to this package and which imports the "game" package. If I create a new file and write import game;
then Eclipse complains that it does not know what the "game" package means. Can somebody please help me to solve this problem?
在我的一个目录中,我拥有属于一个包(“游戏”)的所有 .java 文件。现在我想创建一个不属于这个包的 .java 文件,它导入了“游戏”包。如果我创建一个新文件并写入,import game;
那么 Eclipse 会抱怨它不知道“游戏”包的含义。有人可以帮我解决这个问题吗?
回答by Peter Lang
You cannot import a package, you need to import classes from that package:
您不能导入包,您需要从该包导入类:
import game.SomeClass;
import game.SomeOtherClass;
or
或者
import game.*;
to import all classes from that package.
从该包导入所有类。
Eclipse can generate these imports for you if you type the class-names in your code and press Ctrl+Spacefor Code-Completion. Select the correct class there (if more classes with the same name exist in different packages), and the import will be generated.
如果您在代码中输入类名并按Ctrl+Space进行代码完成,Eclipse 可以为您生成这些导入。在那里选择正确的类(如果不同包中存在更多同名的类),将生成导入。
回答by Eric Eijkelenboom
You might also want to use Eclipse's 'Organize imports' shortcut: Ctrl + Shift + O.
您可能还想使用 Eclipse 的“组织导入”快捷方式:Ctrl + Shift + O。
This will find all missing types and add the appropriate imports automatically.
这将找到所有缺失的类型并自动添加适当的导入。
回答by manikandan
import your package in your new java file or create object and get tha java class wat u want from your old package
在新的 java 文件中导入您的包或创建对象并从旧包中获取您想要的 java 类