Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play Framework - ExecutionContext cannot be resolved when trying to map a Promise

WS.url("https://api.humanapi.co/v1/human"+url+"?updated_since="+updatedSince).setHeader("Authorization", "Bearer "+accessToken)
        .setHeader("Accept", "application/json").get().map(
                new Function<WSResponse, JsonNode>() {
                    public JsonNode apply(WSResponse response) {
                        JsonNode json = response.asJson();
                        success(json);
                        return json;
                    }
                }   
    ); 

This displays an error "The type scala.concurrent.ExecutionContext cannot be resolved. It is indirectly referenced from required .class files".

I've tried adding

import scala.concurrent.ExecutionContext;

but then the error just "moves" from the line where the promise is to the top of the file and still won't compile.

I've also tried adding

import play.api.libs.concurrent.Execution.Implicit.defaultContext;

but there is no such thing to be imported.

Play Framework used is 2.4.2.

SBT file:

version := "1.0-SNAPSHOT"

lazy val root = (project in file(".")).enablePlugins(PlayJava)

scalaVersion := "2.11.6"

resolvers ++= Seq(
    "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/",
    "sonatype snapshots" at "https://oss.sonatype.org/content/repositories/releases/"
)

checksums := Nil

libraryDependencies ++= Seq(
  javaJdbc,
  cache,
  javaWs,
  "org.mockito" % "mockito-all" % "1.10.19",
  "commons-codec" % "commons-codec" % "1.10",
  "de.flapdoodle.embed" % "de.flapdoodle.embed.mongo" % "1.48.0",
  "org.mongodb.morphia" % "morphia" % "1.0.0-rc0"
)

libraryDependencies += "org.mongodb" % "mongodb-driver" % "3.0.2"

// Play provides two styles of routers, one expects its actions to be injected, the
// other, legacy style, accesses its actions statically.
routesGenerator := InjectedRoutesGenerator
like image 722
Myzreal Avatar asked Jul 20 '15 07:07

Myzreal


Video Answer


2 Answers

I had the same problem and realized my Play Eclipse setup was incomplete (I missed steps 2&3 below). I retried the setup, this time following all relevant steps. ExecutionContext became resolvable then.

Instructions for Play Eclipse Setup

Original Reference: https://www.playframework.com/documentation/2.4.x/IDE

Updated Reference: https://www.playframework.com/documentation/2.5.x/IDE

1) Append this line to project/plugins.sbt:

addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "4.0.0")

2) Append this line to build.sbt:

EclipseKeys.preTasks := Seq(compile in Compile)

3) Append these lines to build.sbt (assuming you have no Scala sources):

EclipseKeys.projectFlavor := EclipseProjectFlavor.Java           // Java project. Don't expect Scala IDE
EclipseKeys.createSrc := EclipseCreateSrc.ValueSet(EclipseCreateSrc.ManagedClasses, EclipseCreateSrc.ManagedResources)  // Use .class files instead of generated .scala files for views and routes 

4) Save all your project files. Close Eclipse.

5) In command prompt, cd to your project folder and run:

activator "eclipse with-source=true"

6) Open Eclipse. Open your project. Refresh it as follows:

Package Explorer > right-click your_project > refresh

like image 61
Aaron Avatar answered Sep 19 '22 20:09

Aaron


Just install the ScalaIDE Plugin from the Eclipse Marketplace. This'll resolve the issue + allows you to develop Scala.

like image 21
M156 Avatar answered Sep 18 '22 20:09

M156