Adding Jar files to IntellijIdea classpath - intellij-idea

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.

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.

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:
Open Project Structure
Select Modules, then click on the module for which you want the dependency
Choose the Dependencies tab
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'.

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 .

Related

Intellij Idea Can't add Selenium sources.jar file

I started to learn Selenium.
After creating the project I tried to add some jar files.
As you see all required jars were added. But
...sources.jar
looks like Empty Project. Why this is happening?
Probably this happening because ...source.jar file has a multiple files.
Click Libraries from Project Settings
+and add this .jar file here first.
When you added turn back to Modules
After these steps "Empty library" error is gone.

How to use a JAR file in IntelliJ after downloading?

I want to use the import javax.ide in IntelliJ, specifically exploring MetaClass and seeing what it can do. I've downloaded the JAR (198) from https://download.oracle.com/otndocs/jcp/standard_extension_ide-1.0-fr-oth-JSpec/, and I know I'm supposed to somehow add it to dependencies under Project Settings -> Modules -> Dependencies, but I'm not sure what exactly I'm supposed to add from the downloaded folder. The folder containing javax/ide itself is somewhat buried, but I tried adding different levels of directories and none of them worked. The actual folder doesn't even contain java files, it contains a bunch of htmls so I might just be downloading the wrong thing. Thanks for the consideration
I've downloaded the JAR (198) from
It is not a JAR. This is a .zip archive and it does not contain compiled classes in a form of a JAR. It has only source files.
If you are working with a JAR - you need to add such a jar file which contains the needed for your classes into the module's dependencies.
If you do not have the JAR but only have sources - you can create a separate module from these sources and then use it as a module dependency to the module where you want to use these classes.

Why is my library not being recognized after importing my project?

I'm using the JDBC library in my IntelliJ project (mysql-connector-java-5.1.40-bin.jar)
I put it into a folder named lib and used right click > Add as Library to add it as a library. It worked fine.
Later, I zipped the project and moved it to another computer. After unzipping it and importing it in IntelliJ, it gave me the error SQLException: No suitable driver found for jdbc:mysql, which means that it couldn't find the library.
I looked through the file explorer in IntelliJ and it showed both the jar and XML file, and the XML file was pointing to the right location (jar://$PROJECT_DIR$/lib/mysql-connector-java-5.1.40-bin.jar).
I added the jar file as a library again and now it works. But how can I make it work without having to readd it each time?
Use Gradle (or Maven) for building and dependency management. IDEA should import a Gradle (or Maven) project just fine.
The problem was that I was overwriting the IntelliJ Module Library (.iml) file.
When you import a project to the same folder, IntelliJ asks you whether you want to overwrite or reuse the IML file. Since Overwrite was the default option, I selected it, but that caused it to forget what libraries it was using.
By choosing Reuse instead, it works perfectly.

When deploying Xtext, how to integrate with Eclipse via plugin?

I have finished a beta of my Xtext and Xtend project. Now I want to integrate it with an Eclipse. Using this new Eclipse I want to be able to choose "New 'mydsl' project", then in this new project I want to have a .jar referenced by default. Also an empty file of mydsl. If it is possible, get rid of adding Xtext nature to project .
There are several non-trivial processes for this question:
To be able to choose "New 'yourDSL' project", you will have to implement your own project wizard. That is, in your plugin.xml file you have to contribute to the extension point "org.eclipse.ui.newWizards" and create a class that must implement the INewWizard interface, you can do so by extending the Wizard class.
That wizard, in the performFinish() task, should create a IJavaProject programatically where you can specify the Natures that you want. You also will have to specify the source and output folders, and configure the classpath (adding the classpath entries) as well. This link goes on more detail on how to create the project. http://www.pushing-pixels.org/2008/11/18/extending-eclipse-creating-a-java-project-without-displaying-a-wizard.html
In the previous step (#2), when creating your custom project, you can add packages, folders, files, so you can add your empty .mydsl files.
Now the trickiest part. To have the .jar file referenced by default there are several approaches. The easiest one is if you know the .jar path beforehand, you can add the reference by adding (in step #2) a classpath entry of type CPE_LIBRARY that point to that .jar file, but again you need to know the path somehow. In my case I didn't know the path because my .jar file was a library exported as plugin, so I ended up by implementing an IClasspathContainer that looks up for my plugin-library (.jar file) among the installed plugins. You can look up for your a plugin by doing:
Bundle bundle = Platform.getBundle("com.my.libraryPlugin");
File bundleFile = FileLocator.getBundleFile(bundle);
... and then, in step #2 what I add is a classpath entry of type CPE_CONTAINER:
JavaCore.newContainerEntry("MY_CONTAINER");
You can read more about IClasspathContainer here: http://www.ibm.com/developerworks/opensource/tutorials/os-eclipse-classpath/section2.html
5- You probably know how to do this already. The last step is to export your DSL plugins and copy them in the "plugins" folder in the Eclipse installation. To export the plugins: Select the projects -> Right-click -> Export -> Plug-in Development -> Deployable plug-ins and fragments...

How to generate a jar from a java module in IntelliJ with the dependencies placed in a separate directory?

I have a module for which I want a jar to be created. So, here is what I do:
Adding a jar artifact, using the "From module with dependencies ..." option
In "Jar files from libraries" I select "copy to the output directory and link via manifest"
Leave all the defaults.
After that I can build the jar and all the dependencies would be placed near it in the same directory. So far so good.
Now I wish all the dependencies to be placed in a separate directory near the final jar. So, I repeat the same steps, but this time I create a new directory under the node in the artifact Output Layout tab (using the "Create Directory" button). Next I drag all the dependencies onto the new folder and apply the changes.
On the surface, everything is cool - the dependencies are indeed placed in the dedicated directory, but the MANIFEST.MF file is never updated! It should reference the dependencies via the new directory. As of now, the produced jar cannot be run - its dependencies are not visible.
This is unlike Eclipse, which does make sure the manifest file is correct when exporting a project as jar.
How can I workaround this problem, given that:
I want all the dependencies in a dedicated folder.
I want to use IntelliJ IDEA
I do not want to edit the manifest file manually.
I am using the latest download of the IntelliJ IDEA for windows - 11.1.2, build 117.418
Thanks.
At the moment you have to update the classpath manually in the artifact configuration dialog so that it includes the subdirectory:
I've created an issue for this limitation, please star/vote.