Software Development

What is the correct way to add external JAR files to an IntelliJ IDEA project in 2023?

KI Asked by Kimberly Thompson · 22-03-2023
0 upvotes 14,881 views 0 comments
The question

I'm trying to include a third-party library in my Java project within IntelliJ IDEA, but I keep getting "Class not found" errors at runtime. What is the official, correct way to add these .jar files to the project structure so they are recognized by the compiler and included in the artifact build? Should I be adding them as global libraries, or is it better to attach them directly to specific modules through the dependencies tab?

3 answers

0
SA
Answered on 24-03-2023

The most reliable way to add a JAR is through the "Project Structure" menu. Go to File > Project Structure (or press Ctrl+Alt+Shift+S). Under Project Settings, select Modules, then navigate to the Dependencies tab. Click the green "+" icon on the right, select "JARs or Directories...", and browse to your downloaded file. For better organization, I highly recommend creating a lib folder in your project root and moving your JARs there before adding them. This ensures the paths are relative to your project, making it easier to share the code with other developers. After adding, ensure the scope is set to "Compile" so it’s available during both development and runtime. 

0
BR
Answered on 05-04-2023

If I add a JAR this way, will it automatically be included in my final executable JAR when I build the artifact, or do I need to manually configure the artifact layout as well? 

MA 07-04-2023

Brian, simply adding it as a dependency isn't enough for the final export. You must also go to Project Structure > Artifacts and ensure the library is added to the "Output Layout." If you see the library under "Available Elements" on the right, you need to right-click it and select "Extract into Output Root" or "Put into Output Root." Without this step, your code will compile fine in the IDE but will throw a NoClassDefFoundError the moment you try to run the standalone JAR file on another machine.

0
D
Answered on 15-08-2023

For any modern project, you really should use Maven or Gradle. You just add a line to your pom.xml or build.gradle, and IntelliJ handles the download and classpath setup automatically. 

KI 17-08-2023

I agree with David. While manual addition works for small learning projects, using a build tool is the industry standard. It avoids the "works on my machine" syndrome and manages transitive dependencies which can be a nightmare to track manually.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session