java 如何从不同文件夹中的包导入类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16362350/
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 class from a package in a different folder?
提问by Hello
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.
我是 Java 新手。这是一个关于包的基本问题。我在 Netbeans IDE 中有一个名为“stacklist.java”的小型 Java 项目。它的包名是 stacklist。它有4个不同的类。其中之一是 ListNode。
Now i need ListNode object in other project "queuelist.java".
现在我需要其他项目“queuelist.java”中的 ListNode 对象。
directory structure is StackList->src->stacklist and QueueList->src->queuelist. Both StackList and QueueList are at the same level.
目录结构为 StackList->src->stacklist 和 QueueList->src->queuelist。StackList 和 QueueList 处于同一级别。
And added the folder(StackList\src) in Libraries of queuelist.java project. I did "import stacklist.*;"
并在 queuelist.java 项目的 Libraries 中添加了文件夹(StackList\src)。我做了“导入堆栈列表。*;”
When i run "clean and build project", i am getting this: "error: package stacklist does not exist import stacklist.*;"
当我运行“清理并构建项目”时,我收到以下信息:“错误:包堆栈列表不存在导入堆栈列表。*;”
Please suggest me.
请建议我。
回答by Joop Eggen
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.
您可能会选择Maven,这是一种流行的专业构建基础架构,可帮助处理来自 Internet 的库和库版本控制。和编程约定。
For maven:
对于 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.
开发两个项目需要小心。如果一个项目提供了一个库 StackList.jar,那么你需要保持这个库是最新的。IDE 通常采用快捷方式,但显式使用库可能会产生版本错误。
回答by Hello
Adding StackList.jar file and removing the folder(StackList\src) from the Libraries of current project made this to run without errors.
添加 StackList.jar 文件并从当前项目的库中删除文件夹 (StackList\src) 使其运行没有错误。