Robert Oschler Robert Oschler - 2 months ago 22
Groovy Question

IntelliJ can't find package imported via New Module feature, then automatically deletes import statement?

I'm new to the IntelliJ project. Here is my IntelliJ version info:

IntelliJ IDEA 2017.2.1
Build #IU-172.3544.35, built on July 31, 2017
Subscription is active until April 3, 2018
JRE: 1.8.0_152-release-915-b6 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Linux 3.13.0-128-generic


I am running on Linux Ubuntu 14.04.

I had two completely separate projects. I wanted to import code that resides in a JAR file from the first project into the second.

I tried using the "New Module -> Import from existing sources" option, and that did add the entire first project to my second project's source structure. But I still can't import classes found in the JAR file that are used successfully by the first project, into the second project. When I try, IntelliJ gives me the "cannot resolve symbol" error. More annoyingly, after a certain length of time or when I try to compile the project it automatically deletes the import statement(s). (Is this the "auto-remove unused imports" feature biting me here?)

For example, the following import statement can be found in the first project and is considered valid by IntelliJ (no warnings or errors):

import com.github.**messenger4j**.MessengerPlatform;


But when I try to use that very same import in a file in my second project I get the "cannot resolve symbol" error for "messenger4j and the import statement gets auto-deleted at some point. Note, the file that I'm trying use the import statement in is a groovy source file that resides in the second project, in case that matters.

Also, if I look at the repo repository that IntelliJ maintains in the "~/.m2" directory, I do see the JAR file. Here you can see that I can successfully navigate the ".m2" repository tree to the messenger4j package:

/home/user9/.m2/repository/com/github/messenger4j$


How can I properly import and use the github.com.messenger4j jar file in my second project?

UPDATE: The following appears to give me access to the desired JAR that resides in the "~/.m2" directory:

File ->
Project Structure ->
Dependencies (tab) ->
"+" sign in right hand border ->
Library


I then see the JAR file in the library list and by adding it, I no longer get the unresolved symbol error. Is this the correct way to use IntelliJ/Maven to use an external JAR file?

Answer Source

Is this the correct way to use IntelliJ/Maven to use an external JAR file?

No, this is not the Maven way.

If both projects use Maven they both have a POM file defining a couple of things. Let's say you have the following setup:

Project A

<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>projectA</artifactId>
  <version>1.0-SNAPSHOT</version>

</project>

Project B

<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>projectB</artifactId>
  <version>1.0-SNAPSHOT</version>

</project>

If you want to use classes from project A in Project B, you need to add project A as a dependency of Project B. Change the POM file of project B to:

<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>projectB</artifactId>
  <version>1.0-SNAPSHOT</version>

  <dependencies>
    <dependency>
      <groupId>com.example</groupId>
      <artifactId>projectA</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>

</project>

But that's not the only thing. For project B to use project A, Maven needs to know where it can find it. This is solved by installing project A in the local Maven repository by running the mvn clean install command in the root directory of project A.

After that, IntelliJ's Maven will be able to find the dependency without you manually importing project A.