1

I am using Maven in my project, and for some reasons, some additional jars should be added manually (I have followed the step like Correct way to add external jars (lib/*.jar) to an IntelliJ IDEA project).

The package can be imported successfully. However, the compilation error happens, which indicates the package does not exist and cannot find the symbol of the used object.

I have tried the following tips but it remain unchanged:

  1. Invalid caches / restarts
  2. reimport
  3. delete .idea file and .iml file

The scenario is quit similar to this one : https://intellij-support.jetbrains.com/hc/en-us/community/posts/206821195--beginner-question-including-external-jar-compile-error.

Please see the following sample images. It may run successfully but cannot be compiled well.

IntelliJ screenshot

Maven compile error screenshot

Uwe Allner
  • 3,399
  • 9
  • 35
  • 49
  • In a Maven project, you cannot add jars "manually". Your jars should be in a Maven repository, may it be a Nexus/Artifactory, or a local one. – J Fabian Meier Jul 18 '19 at 07:18
  • I think you need https://mvnrepository.com/artifact/com.alibaba/fastjson/1.2.47 in you dependency. – seenukarthi Jul 18 '19 at 07:30
  • As mentioned, install this jar into your Maven local repository and add it as a maven dependency in your pom.xml into `dependencies` section. – Andrey Jul 18 '19 at 10:56

1 Answers1

4

The reason is that when you add a library manually via IntelliJ, only IntelliJ knows about them and when you compile your code using Maven, it can't be find by Maven because Maven only searches for dependencies you defined in pom.xml.

You should install your libraries in your (at least) local maven repository and add them as a normal dependency in your pom.xml. Then you don't need to add them manually in IntelliJ.

You should follow the steps mentioned at Guide to installing 3rd party JARs

Update: Also you should note that if you're working as a team, you should install this on the local maven repository of all developers (which is not practical). The best solution is to install a Maven repository (e.g. Nexus, Artifactory or Archiva) in a server on your local network and upload your private jar files on those servers. Then all developers can define the address of the local Maven repository server in their local Maven settings and use artifacts/libraries from that servers. Plus it works as a local cache/proxy to fetch any Maven artifacts and prevents unnecessary calls to public maven repositories.

zaerymoghaddam
  • 3,037
  • 1
  • 27
  • 33
  • Thank you for your kind answer. The jar I am going to add on to my project is a private custom one instead of a public library. In this scenario, do I also have to install it to make it visible to Maven? I am trying to install it through the instruction. However, it seems not to work well. – Wayne Bruce Jul 18 '19 at 08:44
  • What error do you get? Do you get error in installation process or when you want to use it? – zaerymoghaddam Jul 18 '19 at 08:44
  • Consider the following please. I am sure the path is correct since I copy and paste it on to the command line. ================== [ERROR] The goal you specified requires a project to execute but there is no POM in this directory (/Users/). Please verify you invoked Maven from the correct directory. -> [Help 1] [ERROR] ... [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MissingProjectException – Wayne Bruce Jul 18 '19 at 08:48
  • What's the command you're using to install? I tried with the exact same command mentioned in the "Guide to installing 3rd party JARs" and installed one of my sample jars successfully. Here is the one I used: `mvn install:install-file -Dfile=sample-test.jar -DgroupId=test -DartifactId=crm -Dversion=1.0.0 -Dpackaging=jar`. I ran it in the same folder that my sample-test.jar existed. – zaerymoghaddam Jul 18 '19 at 08:53
  • I am wondering if there is any other way to allow the compiler to scan jars that are not stored in maven repository. Or it is confined to the repository since I am using Maven project. – Wayne Bruce Jul 18 '19 at 08:56
  • Yes, i was using the same command you mentioned. It works for public jars but not for my custom jars. – Wayne Bruce Jul 18 '19 at 08:58
  • 1
    As long as you're using Maven for the build, there is no other way to add custom dependencies. I've updated my answer with more details about having a local maven servers. Maybe it could help you to do this using a GUI of a local network repository. – zaerymoghaddam Jul 18 '19 at 09:13
  • 1
    Thank you for your answer. Now I have a better understanding of Maven. – Wayne Bruce Jul 18 '19 at 10:39