Import .jar to IntelliJ Java project - java

I made a customised data structures class library and I want to pack it as a .jar file and then use it on another project. My IDE is IntelliJ.
How can I make the .jar file and then import it on my other project?

Open the project that you want to pack as a jar.
Go to File -> Project structure.
Select the Artifacts menu.
Click on the green plus and select JAR -> From modules with dependencies... click Apply and then OK.
Go to Build -> Build artifacts... -> Build
Copy the .jar file that can be found in out/artifacts/<name>/<name>.jar
Open your other project where you want to use your jar file.
Create a folder named lib/ and put your jar file there.
Go to File -> Project structure.
Select the Modules menu on the left and the Dependencies tab.
Click the green plus on the right side, select JARs or directories... and then select your jar file.
You are ready to go.

Related

Using jar file in another project

I have one jar file, named pwd.jar and I want to use that in another project named sample.
How can I do that? What are all the steps I should perform?
Note: I am using Eclipse IDE
Right click your project-> properties->Java Build Path -> Libraries tab -> Add External jar, and browse your jar
Cheers

Add .jars in alternative folder

I am new to Eclipse and Java, and I know that .jar files I need to add into libs derectory so that compiler could recognize them. How I can add .jars not only in libs folder but also in other folder too. How can I configure Eclipse to do that?
It's easiest if you have the jar files visible in the package explorer to start with, i.e. within your project directory. At that point, you can just right-click on the jar file, go to the "Build path" section of the context menu, then select "Add to build path".
If the jar file is elsewhere and you don't want to move it, you can right-click on the project, and under the "Build path" part of the context menu, select "Add external archives...". Then find the jar file in the file browser, and hit OK.
All of this can also be done from the project properties dialog, in the Java Build Path section.
Right Click project
Select Properties
Select Java Build Path
Under Tab Libraries Click on add jars or add external jar to add the required jars
The Java build path is used while compiling a Java project to discover dependent classes . It is made up of the following items:
Code in the source folders
Jars and classes folder associated with the project
Classes and libraries exported by projects referenced by this
project
Our goal is to feed our classes with the dependent classes present in the jars during compile time. Eclipse provies with number of easy ways to do it .Here you can find a good article about how to add the jars in the projects with screenshorts attached to it http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-(Java)

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

Eclipse. Copy JAR file into project

How to include (NOT ONLY reference) JAR file into existing project? I added it by using Project properties - Build path - add external jar's. But when I export my project, and then import it in another computer, this library was missing.
Drag it into your project view so it appears in the project as any other file.
Right-click the jar in the project view and add to build path.
Put your jars into a directory of your project e.g yourproject/lib so that external libraries are accessed through a relative path. And then use "Add JARs" option to add the jar to build path as in eboix's link.
You want to export your project as an archive (Export -> General -> Archive File), and then import your project as an existing project (Import -> General -> Existing Projects into Workspace). This way the project will be identical to the exported version and all of the configuration will be right.
Oh, and do what the other people suggest and copy the Jar file into the project so you don't have to reference it as an external Jar file (and everything is in one place).

a simple question about lib folder in Eclipse

I am new to Eclipse IDE, I created a new project named "Hello World", and I need some APIs which are contained in a jar file.
I created a folder called "lib" under the project folder "Hello World" (parallel with src folder), and I import the .jar file into this lib folder, it extracts all the things to this lib folder automatically.
Then I created my HelloWorld.java class under src folder, however, when I put import ro.xxx.xxx.xxx, eclipse complains "the import ro cannot be resolved". Actually, the ro is under the lib folder.
I am stuck here. Shall I make this lib folder under the src folder instead of parallel with it? Please kindly give me some suggestions, thanks in advance!
Project -> properties -> Java build path -> libraries -> add external jars
Just Putting jar in lib folder won't work , you need those jars in your class path.
Here it is described how to add jars in to your build path in eclipse
Add the library to your build path.
Right mouse button on your project --> Build Path --> Configure Build Path...
Libraries --> Add jar
After you first create the lib folder parallel to the src folder you need to go back into Eclipse and Refresh the project so that Eclipse finds the new folder and jars. You can either highlight the project and hit F5, or use the right click menu off the project. After that the lib folder and jar files appear and you can right click a jar to add to the build path. If you add new jars subsequently you need to refresh again for Eclipse to find them.

Categories