How to import a jar package into my IntelliJ [duplicate] - java

When creating a new Java project in IntelliJ IDEA, the following directories and files are created:
./projectname.iml
./projectname.ipr
./projectname.iws
./src/
I want to configure IntelliJ IDEA to include my dependency JARs in ./lib/*.jar to the project. What is the correct way to achieve this in IntelliJ IDEA?

Steps for adding external jars in IntelliJ IDEA:
Click File from the toolbar
Select Project Structure option (CTRL + SHIFT + ALT + S on Windows/Linux, ⌘ + ; on Mac OS X)
Select Modules at the left panel
Select Dependencies tab
Select + icon
Select 1 JARs or directories option

IntelliJ IDEA 15 & 2016
File > Project Structure...
or press Ctrl + Alt + Shift + S
Project Settings > Modules > Dependencies > "+" sign > JARs or directories...
Select the jar file and click on OK, then click on another OK button to confirm
You can view the jar file in the "External Libraries" folder

Just copy-paste the .jar under the "libs" folder (or whole "libs" folder), right click on it and select 'Add as library' option from the list. It will do the rest...

If you are building your project with gradle, you just need to add one line to the dependencies in the build.gradle:
buildscript {
...
}
...
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
}
and then add the folder to your root project or module:
Then you drop your jars in there and you are good to go :-)

You add them as libraries to your module.
I usually have a /lib directory in my source. I put all the JARs I need there, add /lib as a library, and make it part of my module dependencies.
2018 update: I'm using IntelliJ 2017/2018 now.
I'm fully committed to Maven and Nexus for dependency management.
This is the way the world has gone. Every open source Java project that I know of uses Maven or Gradle. You should, too.

I use this method and it works well:
1- Copy And paste the .jar files under the libs folder.
2- Add compile fileTree(dir: 'libs', include: '*.jar') to dependencies in build.gradle then all the jars in the libs folder will be included..
3- Right click on libs folder and select 'Add as library' option from the list.

Libraries cannot be directly used in any program if not properly added to the project gradle files.
This can easily be done in smart IDEs like inteli J.
1) First as a convention add a folder names 'libs' under your project src file. (this can easily be done using the IDE itself)
2) then copy or add your library file (eg: .jar file) to the folder named 'libs'
3) now you can see the library file inside the libs folder. Now right click on the file and select 'add as library'. And this will fix all the relevant files in your program and library will be directly available for your use.
Please note:
Whenever you are adding libraries to a project, make sure that the project supports the library

Some great help found here. However, I still could not make it to work despite loading JAR properly. I found out later that I accidentally created module in the file structure instead of regular folder and this very module was pre-selected in the project setting.
Here is the footprint:
File -> Project Structure -> Modules -> (select proper module if you have more) -> Dependencies -> + -> JAR or Libraries

While I agree with the previous answers, it's important to note how to access the code of those external libraries.
For example to access a class in the external library, you will want to use the import keyword followed by the external library's name, continued with dot notation until the desired class is reached.
Look at the image below to see how I import CodeGenerationException class from the quickfixj library.

File > Project Structure
Project Settings > Modules > Dependencies (Select one of)
1 JARs or Directories...
2 Library...
3 Module Dependency...
Apply + Ok
Import into java class

You can put the JAR in the libs folder and add it from there. This can be done in 2 different ways in IntelliJ:
Right-click on the libs folder and add from there:
Add the JAR from the project structure:

If you are building your project with maven, you just need to add one line to the dependencies in the pom.xml:
<dependency>
<groupId>com.xxx</groupId>
<artifactId>xxxx-server</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${pom.basedir}/src/libs/xxx-server-1.0.0.jar</systemPath>
</dependency>
and then add the folder to your root project or module:
this is my personal experiences. I wish they would help you

Related

Debugging tomcat filter chain in intellij [duplicate]

When creating a new Java project in IntelliJ IDEA, the following directories and files are created:
./projectname.iml
./projectname.ipr
./projectname.iws
./src/
I want to configure IntelliJ IDEA to include my dependency JARs in ./lib/*.jar to the project. What is the correct way to achieve this in IntelliJ IDEA?
Steps for adding external jars in IntelliJ IDEA:
Click File from the toolbar
Select Project Structure option (CTRL + SHIFT + ALT + S on Windows/Linux, ⌘ + ; on Mac OS X)
Select Modules at the left panel
Select Dependencies tab
Select + icon
Select 1 JARs or directories option
IntelliJ IDEA 15 & 2016
File > Project Structure...
or press Ctrl + Alt + Shift + S
Project Settings > Modules > Dependencies > "+" sign > JARs or directories...
Select the jar file and click on OK, then click on another OK button to confirm
You can view the jar file in the "External Libraries" folder
Just copy-paste the .jar under the "libs" folder (or whole "libs" folder), right click on it and select 'Add as library' option from the list. It will do the rest...
If you are building your project with gradle, you just need to add one line to the dependencies in the build.gradle:
buildscript {
...
}
...
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
}
and then add the folder to your root project or module:
Then you drop your jars in there and you are good to go :-)
You add them as libraries to your module.
I usually have a /lib directory in my source. I put all the JARs I need there, add /lib as a library, and make it part of my module dependencies.
2018 update: I'm using IntelliJ 2017/2018 now.
I'm fully committed to Maven and Nexus for dependency management.
This is the way the world has gone. Every open source Java project that I know of uses Maven or Gradle. You should, too.
I use this method and it works well:
1- Copy And paste the .jar files under the libs folder.
2- Add compile fileTree(dir: 'libs', include: '*.jar') to dependencies in build.gradle then all the jars in the libs folder will be included..
3- Right click on libs folder and select 'Add as library' option from the list.
Libraries cannot be directly used in any program if not properly added to the project gradle files.
This can easily be done in smart IDEs like inteli J.
1) First as a convention add a folder names 'libs' under your project src file. (this can easily be done using the IDE itself)
2) then copy or add your library file (eg: .jar file) to the folder named 'libs'
3) now you can see the library file inside the libs folder. Now right click on the file and select 'add as library'. And this will fix all the relevant files in your program and library will be directly available for your use.
Please note:
Whenever you are adding libraries to a project, make sure that the project supports the library
Some great help found here. However, I still could not make it to work despite loading JAR properly. I found out later that I accidentally created module in the file structure instead of regular folder and this very module was pre-selected in the project setting.
Here is the footprint:
File -> Project Structure -> Modules -> (select proper module if you have more) -> Dependencies -> + -> JAR or Libraries
While I agree with the previous answers, it's important to note how to access the code of those external libraries.
For example to access a class in the external library, you will want to use the import keyword followed by the external library's name, continued with dot notation until the desired class is reached.
Look at the image below to see how I import CodeGenerationException class from the quickfixj library.
File > Project Structure
Project Settings > Modules > Dependencies (Select one of)
1 JARs or Directories...
2 Library...
3 Module Dependency...
Apply + Ok
Import into java class
You can put the JAR in the libs folder and add it from there. This can be done in 2 different ways in IntelliJ:
Right-click on the libs folder and add from there:
Add the JAR from the project structure:
If you are building your project with maven, you just need to add one line to the dependencies in the pom.xml:
<dependency>
<groupId>com.xxx</groupId>
<artifactId>xxxx-server</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${pom.basedir}/src/libs/xxx-server-1.0.0.jar</systemPath>
</dependency>
and then add the folder to your root project or module:
this is my personal experiences. I wish they would help you

Installing additional Java dependencies to react-native library

I want to bring more functionality to an existing react-native library - namely to react-native-maps. In order to do that, I have to add an additional Java library. No I wonder where exactly am I supposed to add the Java library. Do I add here (in a newly created lib folder)
https://github.com/react-community/react-native-maps/tree/master/lib/android or somewhere else? How do I make it "known" to Java?
I think you mean with "Java" your IDE. Depends on what IDE you're using if you happen to use
IntelliJ it goes as follows:
Click File from the toolbar
Project Structure (CTRL + SHIFT + ALT + S on Windows/Linux, ⌘ + ; on Mac OS X)
Select Modules at the left panel
Dependencies tab
'+' → JARs or directories
Eclipse
Start Eclipse, and locate the project folder to which the library
should be added.
Right-click this class folder, and select "Properties"
Select "Java Build Path" on the left, and then the "Libraries" tab.
Now, click the "Add External JARS..." button
Locate and select the .jar file you just downloaded, and then
click "Open"
Finally, click "OK" to close the dialog box.
Netbeans
In the Projects window right-click on the name of the project
Click Properties, The Project Properties window opens.
In the categories tree select "Libraries"
On the right side of the Project Properties window press button "Add
JAR/Folder"
In the case of Gradle you should edit the build.gradle file located at the root of the project. In that file under the section dependency.
Copy From Source
Gradle Guide Android Studio 'Adding dependencies'
Local binary dependency
compile fileTree(dir: 'libs', include: ['*.jar'])
Because Gradle reads paths relative to the build.gradle file, this tells the build system to add all JAR files inside your project's module_name/libs/ directory as dependencies.
Alternatively, you specify individual files as follows:
compile files('libs/foo.jar', 'libs/bar.jar')
Example
dependencies {
def googlePlayServicesVersion = rootProject.hasProperty('googlePlayServicesVersion') ? rootProject.googlePlayServicesVersion : DEFAULT_GOOGLE_PLAY_SERVICES_VERSION
def androidMapsUtilsVersion = rootProject.hasProperty('androidMapsUtilsVersion') ? rootProject.androidMapsUtilsVersion : DEFAULT_ANDROID_MAPS_UTILS_VERSION
compileOnly "com.facebook.react:react-native:+"
compile files('libs/name_of_library.jar') //<----- added external library for gradle to build
implementation "com.google.android.gms:play-services-base:$googlePlayServicesVersion"
implementation "com.google.android.gms:play-services-maps:$googlePlayServicesVersion"
implementation "com.google.maps.android:android-maps-utils:$androidMapsUtilsVersion"
}

Is there any alternate to acquire dependencies without using POM.xml?

My question is, I don't want to write XML code for dependencies in the POM.xml again and again.
For example, I want to use the log4j JAR in my project. instead of writing XML code is there any option to download the related JAR files?
Alternate method is downloading the jars from sites like maven or java2s.com and add them to your build path.
You can add a jar in Eclipse by right-clicking on the Project → Build Path → Configure Build Path. Under Libraries tab, click Add Jars or Add External JARs and give the Jar.
Hope that helps
Of course you can download (or copy) these jars, and then manually add them to your build path.
For instance in eclipse, you can select all the libraries under the "Maven Dependiences" and copy them to other folder eg. in other project. Then you need to select these libraries and using the right mouse button add them to build path, done :)
You do not need to write them again and again. You can define and install a parent pom with the common dependencies you use and refer to it in each project pom.
First you could download jars you need in your project from corresponding websites.
Next,follow the steps below:
Step 1: Create a folder under the project to save the JAR package.We usually store jar packages from the outside in the [lib] folder.
Step 2: Copy the jar to the lib folder.
Step 3: Right-click on the project name, and select it in turn [Build Path] - > [Configure Build Path...]
Step 4: In the open window, select the Libraries page and click on the right button the [add JARs...] then select the jar package we just copied into the project, and then click [OK] to close the window.
Finally, we can use this jar package in Eclipse
Yes you can add them through the java build path from your IDE. However, I would recommend that you add these jars as dependencies in the pom.xml and build your project through maven. This will make sure that you will not run into any error regarding the missing dependencies in case any jar gets deleted accidentally or if you want the same set of jars for a new project. And understanding a pom.xml is not a difficult task

How to Add JAR (HTMLCleaner) to Project in Android Studio 1.1?

I've spent countless hours trying to add a simple JAR (HTMLCleaner) to my project in Android Stuio 1.1 to no avail. I imported the JAR as a module through the interface (File > New Module > Import JAR...), added it as a dependency on my app module, and even reference it in my AndroidManifest.xml <uses-library etc...>.
When I try to "import org.htmlcleaner;" in any of my classes I get a "cannot resolve symbol error." I've researched and tried every suggestion in every permutation and combination. Can anyone offer additional direction or a step-by-step tutorial on the proper way to do this?
Remember that Android Studio uses Gradle as a build tool, and part of the build flow is to handle dependencies. That being said, you have to specify to Gradle that includes your external jar files, making the following modifications:
Open build.gradle file of your module project (generally called "app")
Inside "dependencies", add compile fileTree(dir: 'libs', include: ['*.jar'])
Create a folder called libs inside your module app.
Copy your jar file inside of it
Run the build process of Gradle clicking in the "Sync now" link of Android Studio, and now your library should be recognized.
You can add jar file by following Rodrigo Ayala answer. Also Instead of using jar file you can also add the dependency in your project by add the following lines to build.gradle
dependencies {
// your other dependencies
compile 'net.sourceforge.htmlcleaner:htmlcleaner:2.16'
}
Inside your "app" module create a new folder "libs".
Paste your jar file inside libs folder.
Right Click on the jar file and select "Add as library".
DONE.

Android import library

In my project, I need to use external library. That library consists of .class files. I have added folder with this library to my project using properties -> Java Build Path -> Libraries -> Add class folder. I can compile project without any problem, but when I run it, I get failed resolving xy and debugger stops on line where I create instance of class from imported files.
I guess the external library I use isnt linked to my .apk file. How can I fix this? I have tried to check this library on "Order and Export" tab assuming that, it will export the library with my application. But it didnt work.
How can I link this library to my project .apk? Do I need to somehow install this library on device first? Do I need to copy this library in application folder?
PS: I'm using Eclipse Juno, android SDK target 10.
Thank you
UPDATE
I have this library copied in libs folder. External library path is:
my_project/libs/sk/aicit/leg/libraries*.class
I have added ./libs folder in Libraries settings of project.
I do not have .jar version of this library, only .class files.
I have also souce code of this library, but I didnt want to include its classes in my project, it has its own dependencies, I wanted to just "link" it like library. Its an external library I havent written.
UPDATE 2
I have copied all the source files from library to my projects "src/" dir. I have removed Library from Java build path. I can stil compile my project, but when I run it Iget
FATAL EXCEPTION:main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{
com.example/myvideorecord/com.example.myvideorecord.Robot}: java.langNullPointerException
...
What can I do, how to debug this error? :(
steps
Copy the library file into libs folder
select jar file -> right click -> Build Path -> Add to Build Path
clean the project Project-> Clean -> select your project -> ok
Android Studio
For importing a Android library into Android Studio, use the following steps:
1) (Optional) I like having my main Android project contain everything it needs to compile correctly, so I move external libraries into the libs/ directory. Generally, you only need to keep the following directories:
res
src
AndroidManifest.xml
*.iml (This will typically be the name of the library you're importing.)
2) Go to your Project Properties.
3) Go to Modules.
4) Import a New Module (Command + N and then Import Module).
5) Navigate to the library directory you want to import.
6) Create module from existing sources.
7) Click Next three times to add the necessary files.
8) Click Finish to complete the module additional.
9) Click on your project in the module list and go to the Dependencies tab.
10) Click the + button at the bottom and click Module Dependency....
11) Make sure your library you're importing is selected and click OK.
12) Click OK in the Project Properties window.
And you should be good to go.
Copy that library file into libs folder
if u have the source code of the library you can import it in eclipse .
Right click on your project -> properties->android -> reference-> add
and add the library project which u have imported. This will include only .class files of the library in ur project

Categories