ChatGPT解决这个技术问题 Extra ChatGPT

Correct way to add external jars (lib/*.jar) to an IntelliJ IDEA project

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?

I am facing the issue while running the program in the terminal in IntelliJ. I have used the import statement to use the classes that I require in the program. While I run it in the IDE, it runs fine. But when I try to compile from the command line % javac MyProgram.java, it says error: cannot find symbol
@don_Gunner94 You need to understand how the classpath works and that you may have to provide it more than you expect.
I suggest not to use the approach that you are trying to do. Better you upload your JARs to any artifactory platform, like jFrog and get your artifact from such platform like you do from maven or gradle. I suggest you to follow "The 12-Factor Principles" as "Dependencies (Explicitly declare and isolate the dependencies)"

T
Thorbjørn Ravn Andersen

https://i.stack.imgur.com/ZlENo.png

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


Also, consider adding a lib folder, and use "Attach Jar Directories", which will add all jars in the lib folder. For a largish project, this keeps you from having to choose each jar individually.
For future reference: that "Add" button was off the right edge of the window and I spent an inordinate amount of time looking for it before finding this post and thinking to resize the window. (This was on OSX 10.6.5...)
@joeslice that only works if the JARs are in the directory root, right? It doesn't seem to work for JARs inside sub-directories inside 'lib'.
In IJIdea 13, there is "+"> instead of "add".
I'm using IDEA 14.0.2. Instead of Add there is + symbol. After clicking + there is 3 options: 1. JARs or directories 2. Library... 3. Module Dependency...
R
ROMANIA_engineer

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


I actually create a new Module in the "Modules" pane (named for example "JarsForLogging"), then add all the jars I need (located in some directory far away from the project tree) to this new Module and mark the jars as "Export". Finally, I add the newly created Module to the dependencies of the original Module. A bit more hierarchical that way.
In addition: make sure that file system permissions are set correctly for the .jar files. Use chmod to modify them on Linux or MacOS.
still works in 2018 (year and IntelliJ version). Thank you. This way I can load and use Armed Bear Common Lisp libraries from within IntelliJ, awesome!
Yep. Works well with my IntelliJ as well. Thanks!
@don_Gunner94, via command line, you should also provide the jar like this: javac -cp ".:/my/path/file.jar;" MyProgram.java
u
user1742529

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...

https://i.stack.imgur.com/BwwlE.png


Simplest and the way we're used to do since time of Eclipse.
Plain and Simple!!
Kind of annoying it still asks for a "name" then, can you just leave any junk in there?
This helped me too!
I am facing the issue while running the program in the terminal in IntelliJ. I have used the import statement to use the classes that I require in the program. While I run it in the IDE, it runs fine. But when I try to compile from the command line % javac MyProgram.java, it says error: cannot find symbol. –
c
cesards

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:

https://i.stack.imgur.com/zVzAR.png

Then you drop your jars in there and you are good to go :-)


I am facing the issue while running the program in the terminal in IntelliJ. I have used the import statement to use the classes that I require in the program. While I run it in the IDE, it runs fine. But when I try to compile from the command line % javac MyProgram.java, it says error: cannot find symbol. –
This should be the correct answer, as it let's you push and sync the libraries needed without any configuration from the ones that pull the project. Set it once and forget.
Wow I can't believe how long it took to find this one line! Not even the tutorial on Gradle has this. Took me a week to get this working (in the little spare time and energy I had) haha. I don't create libraries normally and it was so long ago that I did it before.
d
duffymo

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.


Yes, but what if the Maven pom is not called pom.xml? Then it seems like I can't read Maven dependencies.
Convention is to call it pom.xml. Follow convention and rename your file.
I still find the need, when the jar has no maven repo ... but, I do not understand how to do this correctly . I understand how to add a directory, I just don't understand how to get it it import the javadocs also too. it works when I add a jar as a single file, but not if I add a directory. Is it simply not possible?
This is sound advice. Adding jars manually is time consuming and prone to error. Your dependencies should always be configured "via code", in the pom.xml (for maven) or whatever the equivalent is for gradle.
All JARs have a repo. If it's not available in a public or private repo you can mvn install it manually in your local .m2. There's no reason to do otherwise.
A
Ali Hesari

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.


Thank you, this was the only thing that worked for me!
I am facing the issue while running the program in the terminal in IntelliJ. I have used the import statement to use the classes that I require in the program. While I run it in the IDE, it runs fine. But when I try to compile from the command line % javac MyProgram.java, it says error: cannot find symbol.
K
Keet Sugathadasa

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


R
Rolland

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


k
kgui

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.

https://i.stack.imgur.com/d9Cfj.png


j
jumping_monkey

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

https://i.stack.imgur.com/UPJVk.png

Import into java class


A
Arefe

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:

https://i.stack.imgur.com/JnhIj.png

Add the JAR from the project structure:

https://i.stack.imgur.com/mSQcH.png


q
qingmu

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:

https://i.stack.imgur.com/jQnyF.png

this is my personal experiences. I wish they would help you