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

4

7
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
Cere answered 20/7, 2015 at 7:42 Comment(5)
I did but nothing in there helps me solve the problem.Cere
In most situations, the appropriate execution context to use will be the Play default thread pool. This can be used by importing it into your Scala source file: import play.api.libs.concurrent.Execution.Implicits._ playframework.com/documentation/2.4.x/…Aldas
I know that. The issue is that "the import play.api.libs.concurrent.Execution.Implicits._ cannot be resolved".Cere
Does it compile in SBT? If it does, you're fine. It might be eclipse related.Fib
It is indeed Eclipse related. I had the same issue and everything compiled but eclipse was having a fit about it! This was quite annoying as it broke all Eclipse goodies that I rely on like automatically arranging imports (Eclipse would delete various imports for things it would no longer 'see', but then this did in fact break the build). The fix is given by Aaron, you just need to make sure that you do not skip any of the steps given when setting up Eclipse.Carce
T
9

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

Thrown answered 4/8, 2015 at 3:45 Comment(4)
Thanks, I'll try this and amend the answer if it helps.Cere
@JayQ. I'm not sure what to suggest other than to try creating a new blank Play app and following the Eclipse setup instructions closely.Thrown
You're right, it must be a problem specific with my project. I created a new project and it worked. I followed playframework.com/documentation/2.4.x/IDE which is basically what you posted.Ouster
This worked for me, this should be the accepted answer.Carce
F
1

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

Fabrin answered 4/11, 2015 at 12:17 Comment(1)
This does the job and it seems to be The Right Thing to do. Works even if you have (for what reason soever) an outdated SBT Eclipse plugin and an older Play in the path - we're still stuck with sbt-eclipse-plugin 3.0.0 and Play 2.4.Mayes
C
0

Solution:

import scala.concurrent.ExecutionContext;

Ignore the eclipse errors.

Cere answered 22/7, 2015 at 7:45 Comment(2)
Ignoring the "cannot be resolved" issue can compromise (disable) Eclipse code assist. Please see below for what worked in my case.Thrown
This solution is simply not acceptable, see Aaron's answer below.Carce
S
0

also try removing the eclipse stuff from the source directory (.project, .classpath, etc) and re-import the project.

Subservient answered 1/9, 2016 at 18:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.