How to import a package from Eclipse?

微笑、不失礼 提交于 2019-12-19 05:54:14

问题


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?


回答1:


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+Space for Code-Completion. Select the correct class there (if more classes with the same name exist in different packages), and the import will be generated.




回答2:


You might also want to use Eclipse's 'Organize imports' shortcut: Ctrl + Shift + O.

This will find all missing types and add the appropriate imports automatically.




回答3:


import your package in your new java file or create object and get tha java class wat u want from your old package



来源:https://stackoverflow.com/questions/2681161/how-to-import-a-package-from-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!