54

I have created a new Intellij project. But I can't use the third party jars in my project. I have the jars in a directory structure as follows:
repository/commons-logging/commons-logging-1.0.4.jar
repository/commons-lang/commons-lang-2.1.jar
etc.

I want to add them just by pointing to the repository directory, but couldn't find any ways to add them. But when I am using classes or API from that jars, the editor can't resolve the classes. I have tried from Project Structure but can't configure it.

Kara
  • 6,115
  • 16
  • 50
  • 57
java developer
  • 484
  • 1
  • 5
  • 9
  • 1
    Refer to the documentation for adding the module dependencies: http://www.jetbrains.com/idea/webhelp/configuring-module-dependencies-and-libraries.html. – CrazyCoder May 24 '13 at 19:13
  • Use maven to manage your dependencies. Putting jars into a directory is bad practice. – kan May 24 '13 at 19:14
  • 3
    Don't need Maven; I'd consider it to be a bad practice. – duffymo May 24 '13 at 19:15

3 Answers3

74

Go to File-> Project Structure-> Libraries and click green "+" to add the directory folder that has the JARs to CLASSPATH. Everything in that folder will be added to CLASSPATH.

Update:

It's 2018. It's a better idea to use a dependency manager like Maven and externalize your dependencies. Don't add JAR files to your project in a /lib folder anymore.

Screenshot

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 1
    Thanks duffymo for your response. I have followed the steps you mentioned and the jars added in the External Library section. However when I am trying to use them i.e. any class inside any external jar, the compiler can't detect the class. Is there any more steps to follow? Please suggest. – java developer May 25 '13 at 17:55
  • You did it wrong, then. See the picture. Click on the plus sign and add the directory where the JARs are. – duffymo May 25 '13 at 19:47
  • 45
    Here's an idea... for those new to IntelliJ, it's from the File menu: File -> Project Structure -> Libraries... I spent half an hour trying to locate the Project Structure dialog in the interface based on this post and similar ones elsewhere in stackoverflow.com. – Russ Bateman Apr 10 '14 at 15:38
  • 1
    For those on Windows, just hit `ctrl-alt-shift-s`. – kurtzbot Aug 08 '14 at 14:24
  • 1
    ... or `F4` (also on Windows) – andreybavt Feb 06 '15 at 21:19
  • This doesn't work on PHPStorm to add Clojure to the classpath for copying JSON versions of MySQL tables from the database browser. – CommaToast Nov 05 '16 at 00:16
  • 1
    @duffymo, How can I modify the part after `-cp` **directly**? – Pacerier Dec 10 '17 at 01:25
9

If, as I just encountered, you happen to have a jar file listed in the Project Structures->Libraries that is not in your classpath, the correct answer can be found by following the link given by @CrazyCoder above: Look here http://www.jetbrains.com/idea/webhelp/configuring-module-dependencies-and-libraries.html

This says that to add the jar file as a module dependency within the Project Structure dialog:

  1. Open Project Structure
  2. Select Modules, then click on the module for which you want the dependency
  3. Choose the Dependencies tab
  4. Click the '+' at the bottom of the page and choose the appropriate way to connect to the library file. If the jar file is already listed in Libraries, then select 'Library'.
vinegarbin
  • 120
  • 1
  • 7
1

On the Mac version I was getting the error when trying to run JSON-Clojure.json.clj, which is the script to export a database table to JSON. To get it to work I had to download the latest Clojure JAR from http://clojure.org/ and then right-click on PHPStorm app in the Finder and "Show Package Contents". Then go to Contents in there. Then open the lib folder, and see a bunch of .jar files. Copy the clojure-1.8.0.jar file from the unzipped archive I downloaded from clojure.org into the aforementioned lib folder inside the PHPStorm.app/Contents/lib. Restart the app. Now it freaking works.

EDIT: You also have to put the JSR-223 script engine into PHPStorm.app/Contents/lib. It can be built from https://github.com/ato/clojure-jsr223 or downloaded from https://www.dropbox.com/s/jg7s0c41t5ceu7o/clojure-jsr223-1.5.1.jar?dl=0 .

CommaToast
  • 11,370
  • 7
  • 54
  • 69