Issue
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?
In Eclipse I would simply go to ProjectB's Build Path settings and add ProjectA.
Solution
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.
Steps in IDEA ( You won't need these below steps if you follow below mentioned best practices):
- Right click on project and select open module settings
- Go to dependencies tab
- click plus sign and add the module you want to use.
Best practices:
- Never use project class in another project, always create a nice interface and use that interface in other projects.
- If possible use Dependency Injection to manage different projects and their dependencies (this internally uses interfaces to do this)
- Use build tool like ant/maven/ivy etc to manage build process.
- Enjoy :)
Answered By - user4948585
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)