How to import a class from a package in a different folder?

南笙酒味 提交于 2019-12-11 18:53:15

问题


I am new to java. This is a basic question about packages. I have a small java project named "stacklist.java" in Netbeans IDE. It's package name is stacklist. And it has 4 different classes. One of them is ListNode.

Now i need ListNode object in other project "queuelist.java".

directory structure is StackList->src->stacklist and QueueList->src->queuelist. Both StackList and QueueList are at the same level.

And added the folder(StackList\src) in Libraries of queuelist.java project. I did "import stacklist.*;"

When i run "clean and build project", i am getting this: "error: package stacklist does not exist import stacklist.*;"

Please suggest me.


回答1:


For

package a.b.c;
public class D;

package e;
import a.b.c.D;
public class E;

you need

src\a\b\c\D.java
src\e\E.java

You might go for Maven, a popular professional build infrastructure which helps with libraries from the internet and library versioning. And programming conventions.

For maven:

package a.b.c;
public class D;

package e;
import a.b.c.D;
public class E;

you need

src\main\java\a\b\c\D.java
src\main\java\e\E.java

Developing two projects needs care. If one project gives a library StackList.jar then you need to keep this library builded up to date. Often an IDE takes a shortcut, but the explicit use of a library may yield version errors.




回答2:


Adding StackList.jar file and removing the folder(StackList\src) from the Libraries of current project made this to run without errors.



来源:https://stackoverflow.com/questions/16362350/how-to-import-a-class-from-a-package-in-a-different-folder

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