IntelliJ Doesn't Properly Import Jars
Asked Answered
E

10

21

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

Earnest answered 15/8, 2012 at 2:13 Comment(3)
Make sure the "scope" is set to Compile, not Runtime/Provided/Test.Reorder
Try to invalidate caches from File menuBasiliabasilian
I know it's old. But invalidate cache worked for me.Gezira
P
14

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.

Piranesi answered 15/8, 2012 at 10:0 Comment(6)
So I redid everything again, but no dice. I can see the jars and the classes inside in the project view and they're properly added under Module Dependencies, with compile scope, but I still can't use them. I tried invalidating caches, which also didn't help.Earnest
Please provide a project to reproduce.Piranesi
I've got a bare project, with a 'lib' folder under the project directory with the 2 jarfiles, and a 'src' folder. Under the 'src' folder is a package 'main', and under that package is a package 'week1', where I've got the file I'm working on. For some reason, if I put 'import main.*' above the package declaration in the source file I"m working on, I can see the classes bundled in the jar in code completion, but I get an error for obvious reasons. Am I doing something wrong by putting the jarfiles under the project directory?Earnest
Is there any way then I can get the jarfiles imported across the whole project? I can only use the classes inside if I put my source file in the default package of the project for some reason.Earnest
You really need to share your project and provide more details on the configuration so that we can understand what's wrong with it.Piranesi
Thank you very much. My library was broken and now the new version downloaded working for me)Durham
D
3

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.

Dysgraphia answered 16/9, 2014 at 16:22 Comment(2)
Any suggestions on how to read external libraries from inside a package? I like to avoid using the default package to maintain code organization.Tipcat
This is exactly the issue that I was having. It's annoying, though. Is there a way to still use a package structure while using these sort of (university-created) libraries?Calcify
G
2

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.

Globoid answered 20/4, 2013 at 12:27 Comment(1)
Why is it not wise to recreate a project - it costs nothing to delete and recreate. If you are using maven or gradle - its pretty straightforward to recreate. For maven - run mvn idea:idea , for gradle you have to apply plugin: idea And then gradle ideaProjectDaedal
A
1

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

Antonetteantoni answered 5/7, 2018 at 19:41 Comment(0)
A
0

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.

Annia answered 10/1, 2014 at 16:10 Comment(0)
T
0

Maven projects -> Right click your project -> Unignore

Twickenham answered 15/12, 2014 at 9:50 Comment(0)
S
0

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

Sandal answered 20/12, 2016 at 9:5 Comment(0)
B
0

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>-->
Betatron answered 2/7, 2021 at 10:2 Comment(0)
P
0

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

Pelletier answered 5/3 at 10:5 Comment(0)
M
0

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>
Munch answered 11/3 at 8:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.