Java 如何在 IntelliJ IDEA 中使用另一个项目中的类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31342689/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:56:11  来源:igfitidea点击:

How do I use classes from another project in IntelliJ IDEA?

javaeclipseintellij-idea

提问by Simon Morgan

I have two IntelliJ IDEA Java projects; ProjectA and ProjectB. I want to import and use code from ProjectA in ProjectB. How do I do this?

我有两个 IntelliJ IDEA Java 项目;项目A和项目B。我想从 ProjectB 中的 ProjectA 导入和使用代码。我该怎么做呢?

In Eclipse I would simply go to ProjectB's Build Path settings and add ProjectA.

在 Eclipse 中,我只需转到 ProjectB 的 Build Path 设置并添加 ProjectA。

采纳答案by Ivaylo D. Ivanov

You can create dependency between these projects (Make project B dependent on project A) What it does is essentially compiles project A first then put its compiled jar as dependency to Project B for compiling or running. You can do this manually also.

您可以在这些项目之间创建依赖关系(使项目B依赖于项目A)它所做的本质上是先编译项目A,然后将其编译的jar作为依赖项放到项目B中进行编译或运行。您也可以手动执行此操作。

Steps in IDEA ( You won't need these below steps if you follow below mentioned best practices):

IDEA 中的步骤(如果您遵循以下提到的最佳实践,则不需要以下这些步骤):

  1. Right click on project and select open module settings
  2. Go to dependencies tab
  3. click plus sign and add the module you want to use.
  1. 右键单击项目并选择打开模块设置
  2. 转到依赖项选项卡
  3. 单击加号并添加要使用的模块。

Best practices:

最佳实践:

  1. Never use project class in another project, always create a nice interface and use that interface in other projects.
  2. If possible use Dependency Injection to manage different projects and their dependencies (this internally uses interfaces to do this)
  3. Use build tool like ant/maven/ivy etc to manage build process.
  4. Enjoy :)
  1. 永远不要在另一个项目中使用项目类,始终创建一个漂亮的界面并在其他项目中使用该界面。
  2. 如果可能的话,使用依赖注入来管理不同的项目及其依赖(这在内部使用接口来做到这一点)
  3. 使用 ant/maven/ivy 等构建工具来管理构建过程。
  4. 享受 :)

回答by Paul

you have to create a module of your Project A and add in the path of your project B.

您必须创建项目 A 的模块并添加到项目 B 的路径中。

Follow this link for learn how to create module and use it in other project : Creating a module library

按照此链接了解如何创建模块并在其他项目中使用它:创建模块库

Hope i help you

希望我能帮助你

EDIT : ok so you have to create a module or a package with your Project A a .jar.

编辑:好的,所以你必须用你的项目 A .jar 创建一个模块或一个包。

after do this, go to File -> project structure.

执行此操作后,转到文件-> 项目结构。

On this windows go to Librairies -> click on + and select your module project B.jar.

在此窗口中,转到 Librairies -> 单击 + 并选择您的模块项目 B.jar。

and Now you have to import your librairie in your class like
import projectb.*;

现在你必须在你的班级中导入你的图书馆,比如
import projectb.*;

now instancie the class you want to use and you can use all of the method of this class

现在实例化你想使用的类,你可以使用这个类的所有方法

hope i help you with that precision

希望我能以这种精确度帮助你

回答by Ivaylo D. Ivanov

Eclipse's concepts of "workspace" and "project" are matched by IntelliJ IDEA's "project" and "module". So one way of doing this is to create a project, say ProjectAB, and import your two existing ProjectA and ProjectB as modules, I'll call them ModuleA and ModuleB.

Eclipse 的“工作区”和“项目”概念与 IntelliJ IDEA 的“项目”和“模块”相匹配。因此,这样做的一种方法是创建一个项目,例如 ProjectAB,并将您现有的两个 ProjectA 和 ProjectB 作为模块导入,我将它们称为 ModuleA 和 ModuleB。

Right after that make sure that in the project tree both modules have correct folders marked as "source" folders (in my case they are ModuleA/src/main/java and ModuleB/src/main/java).

紧接着确保在项目树中,两个模块都有正确的文件夹标记为“源”文件夹(在我的例子中它们是 ModuleA/src/main/java 和 ModuleB/src/main/java)。

Then you have to configure ModuleB to depend on ModuleA (ModuleB > Dependencies> Add > Module Dependency).

然后你必须将 ModuleB 配置为依赖于 ModuleA(ModuleB > Dependencies> Add > Module Dependency)。