How to resolve import in Eclipse?

Viewed 314

Im new in Java development and not familiar with various kinds of import (Maven, Git, etc), so I make it simple:

import com.google.common.collect.*;
import com.google.gson.*;

These two is not resolved in code Im inspecting, and I have no idea what kind of actions I should take neither what I should import to resolve it but it is probably some popular library.

Is there complete guide how developers import packages in eclipse (for example C# developers use Nuget, despite there is a ton of hand made ones), or they really use all this enormous import selector?

2 Answers

First of all Mavenise your current project and add the following dependency to it:

Goto: POM.XML after converting your current project to Maven project.

    <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.mycompany.app</groupId>
      <artifactId>my-app</artifactId>
      <version>1.0-SNAPSHOT</version>

      <properties>
       ------Properties Here----
      </properties>

      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.12</version>

        </dependency> 

          <dependency>
        <groupId>com.google.collections</groupId>
        <artifactId>google-collections</artifactId>
        <version>1.0-rc2</version>
    </dependency>

    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.5</version>
    </dependency>
--------Add your Dependencies here, remember you have to add dependencies under <dependencies> here </dependencies> ----------
      </dependencies>
    </project>

Search for all dependencies here: https://mvnrepository.com/ , incase you need more dependencies to import.

How to mavenise your current Java Project:

In your eclipse just right click on Java Project and click Configure and you should see “ Convert to Maven Project ” option.

What is POM.XML

A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project. It contains default values for most projects. Now, you can add a dependency in pom.xml.

If you use no dependency management tool like maven the simplest way is just download corresponding JARs and add them manually: http://www.oxfordmathcenter.com/drupal7/node/44

For GSON use this: https://mvnrepository.com/artifact/com.google.code.gson/gson/2.3.1

For the second dependency I suppose you should use GUAVA: https://github.com/google/guava/wiki/UseGuavaInYourBuild

or this: https://mvnrepository.com/artifact/com.google.collections/google-collections/1.0-rc2

But actually better to convert your project to Maven project (as described for example here: https://crunchify.com/how-to-convert-existing-java-project-to-maven-in-eclipse/) so that you'll be able to use pom.xml for dependency management or Gradle (see configuring existing eclipse java project to build using gradle) and avoid manual JAR download

Related