Jeff Gong
Jeff Gong

Reputation: 1863

Java Beginner Packages in IntelliJ

I am currently attempting to edit a larger java project. After reading suggestions for how to get started, I decided to attempt to build the project first to play around with some of the code and see if I could get an understanding for the hierarchy of the project.

Unfortunately, I can't even seem to get past some of the basic issues such as missing package imports, and I'm not sure what is the best way to fix them. I currently set up my project on IntelliJ to pull from my git repo, and some packages are missing and display an error like:

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.client.methods.CloseableHttpResponse; 
import org.apache.http.HttpEntity;

For each of these import statements, however, the http is underlined in red. I'm guessing this is a missing dependency? What would be the best way to resolve this?

build.sbt file:

import AssemblyKeys._

assemblySettings

organization := ""

name := "service"

version := "1.0"

scalaVersion := "2.10.2"

mainClass in assembly := Some("")

jarName in assembly :=  { s"${name.value}-${version.value}.jar" }

val deployDevTask = TaskKey[Unit]("deploy-dev", "Copies assembly jar to a dev remote location")

val devNode = ""

deployDevTask <<= assembly map { (asm) =>
  val local = asm.getPath
  val remote = devNode + ":" + "/tmp/" + asm.getName
  println(s"Copying: $local -> $remote")
  Seq("scp", local, remote) !!
}

Upvotes: 0

Views: 310

Answers (2)

user1207727
user1207727

Reputation: 1553

The correct answer at the time of writing is to add the line stated by bjfletcher, but with a single % rather than double:

libraryDependencies += "org.apache.httpcomponents" % "httpclient" % "4.5"

From the sbt docs http://www.scala-sbt.org/1.0/docs/Library-Dependencies.html, when using double percentages (%%) sbt will add your project’s Scala version to the artifact name. Often this isn't what you want as a build from a slightly different Scala version will be compatible and there may not be an exact version match available.

Using the single percentage means that you'll get a version even if there isn't an exact match with your Scala version.

Upvotes: 0

bjfletcher
bjfletcher

Reputation: 11518

Add the following to your build.sbt:

libraryDependencies += "org.apache.httpcomponents" %% "httpclient" % "4.5"

Then hover the icon at the bottom-left of IntelliJ IDEA which should bring up a list of different views - select SBT. (If you don't see SBT, go into preferences and add it as a plugin.)

In the SBT view, click the "Refresh" icon (it's the first icon). Watch it downloads the dependencies. It'll popup a message if it can't download the dependency.

Finally the red underscores should disappear.

Upvotes: 1

Related Questions