xjtian
xjtian

Reputation: 1006

IntelliJ Doesn't Properly Import Jars

I'm using IntelliJ 11.1.3 and I'm trying to attach two external libraries in the form of jarfiles to use in a module. I've added them in project structure -> module -> dependencies and they show up correctly under libraries as well, but I can't import them in my source code.

Is there a step I'm missing here? I'm pretty new to IntelliJ, having used Netbeans exclusively before, so I'm a little lost. I thought it would be simple enough to attach the external libraries and use the classes immediately, but I don't even get the library names on code completion when I type 'import'.

Upvotes: 23

Views: 51151

Answers (10)

Dinushika Rathnayake
Dinushika Rathnayake

Reputation: 419

In my case I added dependencies via pom.xml and dependencies are loaded correctly to the external libraries but unable to import. Issue was fixed by changing the build plugging to maven-jar-plugin.

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.2.0</version> <!-- Ensure you're using a recent version -->
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

Upvotes: 0

G&#246;kt&#252;rk
G&#246;kt&#252;rk

Reputation: 69

In my case, I was doing the algorithms course for princeton and they provided the jar as algs4.jar. However, when importing, I had to include the whole package name edu.princeton.cs.algs4, which I learned by decompiling the jarfile. Be sure to check you are importing it correctly and not just by the jar name like me

Upvotes: 0

Dzmitry Darashuk
Dzmitry Darashuk

Reputation: 525

In my case, when I compiled .jar file in POM.xml have a code wich produce EXECUTABLE .jar, but for use .jar with role library in other application must be NOT EXECUTABLE .jar(it have not main.claass and etc. it is not must for us). Actually when I erase this cod from POM.xml - my library start working very well.

-->
<!--                <groupId>org.springframework.boot</groupId>-->
<!--                <artifactId>spring-boot-maven-plugin</artifactId>-->
<!--                <configuration>-->
<!--                    <excludes>-->
<!--                        <exclude>-->
<!--                            <groupId>org.projectlombok</groupId>-->
<!--                            <artifactId>lombok</artifactId>-->
<!--                        </exclude>-->
<!--                    </excludes>-->
<!--                </configuration>-->
<!--            </plugin>-->

Upvotes: 0

MasterJoe
MasterJoe

Reputation: 2355

I got the solution here - https://intellij-support.jetbrains.com/hc/en-us/community/posts/207108385-IntelliJ-not-able-to-resolve-classes-of-external-Libraries-for-Maven-projects

This is what I tried and it worked -

Adding the jar to Classpath seems to help. Project Structure > Platform Settings > SDKs > Classpath Click the + and add the jar

Upvotes: 1

Rites
Rites

Reputation: 2342

I faced the same issue and tried almost all of the solutions mentioned above and here. For most of us, mentioned solutions should work.

My case was a bit different, so thought to mention it here.

In IntelliJ project Maven Repository settings, I found out that even local .m2 repository was unable to update. It tries to connect to localhost and particularly in my case, hosts file had a different setting for localhost.

I changed that and it started working as it was able to connect to local .m2 now

Upvotes: 0

softarn
softarn

Reputation: 5496

Maven projects -> Right click your project -> Unignore

Upvotes: 0

Paul Z.
Paul Z.

Reputation: 153

You're using a package. These libraries are to be used from the default package. So, move all your classes to the default package (i.e. the src folder) and remove all package/import statements. Also, instead of java.io, use the In or Stdin classes to read the data.

Upvotes: 3

TheCoder
TheCoder

Reputation: 8923

If your jar dependency in the POM does not have a scope (or if it has one, change it to a new temporary value), add a temporary one, e.g. 'compile'. Intellij will recognise the change and refresh the External Libraries. You can then remove the temporary scope.

Upvotes: 0

JoM
JoM

Reputation: 43

I met this problem too, what i did is close current project and recreate/import the project and then import jars, and it works. I know its not wise ,but it's a direct and simple way. As i tried all the thing CRAZYCODER said but not work.

Upvotes: 3

CrazyCoder
CrazyCoder

Reputation: 402463

You are doing it right, libraries are configured in the Module Dependencies. Verify that the correct files were added there.

You should be able to browse inside the jars in the Project View under Libraries node. If you can't see the jars there, then your library is not configured properly. If you see the jars, but can't see classes inside of them, they are probably broken, replace them with the valid copies.

Also check that the libraries are added to the dependencies of the correct module where you try to import and use them. Verify the scope of the dependency, Compile is most likely what you need.

File | Invalidate Caches may help in case everything looks correct, but doesn't work.

If nothing helps, please share a sample project with the libraries to illustrate the issue.

Upvotes: 15

Related Questions