Sumanth
Sumanth

Reputation: 605

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

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.

Upvotes: 0

Views: 22159

Answers (2)

Sumanth
Sumanth

Reputation: 605

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

Upvotes: 0

Joop Eggen
Joop Eggen

Reputation: 109547

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.

Upvotes: 4

Related Questions