问题
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