2

In my Play 2.0 Framework Java project, the following line yields errors both in Eclipse and during the sbt compile step:

import javax.inject.*;

I already added the javax.inject dependency to my build.sbt file:

libraryDependencies ++= Seq(
    javaCore,
    javaJdbc,
    javaEbean,
    javaWs,
    javaFooBar,
    cache,
    "javax.inject" % "javax.inject" % "1",
    "org.atmosphere" % "atmosphere-play" % "2.1.1"
)

and executed clean, update & eclipse with-source=true like mad:

[myproject] $ eclipse with-source=true
[info] About to create Eclipse project files for your project(s).
[info] Compiling 3 Scala sources and 12 Java sources to ./myproject/target/scala-2.11/classes...
[error] ./myproject/app/com/elements/legacy/LegacyController.java:3: object inject is not a member of package javax
[error] import javax.inject.*;
[error]        ^
[error] one error found
[error] (compile:compile) Compilation failed
[info] Resolving jline#jline;2.11 ...
[error] Could not create Eclipse project files:
[error] Error evaluating task 'dependencyClasspath': error

I have the feeling that sbt does not throw errors in case a dependency could not be resolved (e.g. javaFooBar above). How can this be activated?

How can I properly build a Play 2.0 Java project using javax.inject?

Thanks a lot!

Edit:

Extending the repository list in project/plugins.sbt in the following way did the trick:

// The repositories
resolvers ++= Seq(
    Resolver.sonatypeRepo("snapshots"),
    Resolver.sonatypeRepo("releases"), 
    Resolver.typesafeRepo("snapshots"), 
    Resolver.typesafeRepo("releases")
)

The dependencies command as described by Donovan is very helpful to check whether a dependency could be resolved or not!

Phil Rykoff
  • 11,999
  • 3
  • 39
  • 63
  • Try removing ivy local cache. – Mon Calamari Apr 28 '15 at 07:09
  • So I removed the complete .ivy2 directory in my home dir, `clean`, `update` & `eclipse with-source=true` and it threw the same error as posted in the question :/ – Phil Rykoff Apr 28 '15 at 07:15
  • 2
    Perhaps you need to add some resolvers. Something like: `resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"` – Carlos Vilchez Apr 28 '15 at 09:21
  • 2
    If you run `[myproject] $ dependencies`, does `javax.inject:javax.inject:1` get resolved ok? – Donovan Muller Apr 28 '15 at 11:30
  • The `dependencies` output does not contain the string `inject`. Thanks for this handy tool! Will now check the resolvers. – Phil Rykoff Apr 28 '15 at 12:07
  • I added the two sonatype resolvers for snapshots and releases as described here: http://www.scala-sbt.org/0.13/docs/Resolvers.html & it works like a charm. Don't forget to refresh your project in eclipse, otherwise eclipse will still show the error as red. – Phil Rykoff Apr 28 '15 at 12:57
  • 1
    This library has been in Maven Central for quite some time. I suspect the resolvers aren't doing anything, it's because you didn't `reload` the project in the activator console after you added the dependencies. If you restarted activator after adding the resolvers, this would have the same effect. From http://www.scala-sbt.org/0.12.4/docs/Getting-Started/Running.html#common-commands : "`reload` Reloads the build definition (build.sbt, project/*.scala, project/*.sbt files). Needed if you change the build definition." – Steve Chaloner Apr 29 '15 at 07:05

1 Answers1

5

This looks like a failure to reload the project definition within activator.

If I update my build.sbt with the following, the project will still compile correctly not because there are no problems with the dependency but because it doesn't know about the changes.

libraryDependencies ++= Seq(
  javaJdbc,
  javaEbean,
  cache,
  javaWs,
  "foo" % "bar" % "1.0"
)

Compile message:

[exampleApp] $ compile
[success] Total time: 0 s, completed 29-apr-2015 9:13:30

If I now reload my project configuration, we'll start to see problems.

[warn]  ::::::::::::::::::::::::::::::::::::::::::::::
[warn]  ::          UNRESOLVED DEPENDENCIES         ::
[warn]  ::::::::::::::::::::::::::::::::::::::::::::::
[warn]  :: foo#bar;1.0: not found
[warn]  ::::::::::::::::::::::::::::::::::::::::::::::
[trace] Stack trace suppressed: run last *:update for the full output.
[error] (*:update) sbt.ResolveException: unresolved dependency: foo#bar;1.0: not found

This is exactly what you would see if you added a dependency that requires a special resolver, e.g. snapshots, etc.

Let's remove that line from build.sbt and reload so we can compile correctly, and then add an import for a package that doesn't exist within the project.

build.sbt (followed by a reload)

libraryDependencies ++= Seq(
  javaJdbc,
  javaEbean,
  cache,
  javaWs
)

Application.java

import play.*;
import play.mvc.*;

import views.html.*;
import foo.bar.*;

public class Application extends Controller {

    public static Result index() {
        return ok(index.render("Your new application is ready."));
    }
}

Compiling this results in

[error] D:\tmp\exampleApp\app\controllers\Application.java:7: error: package foo.bar does not exist
[error] import foo.bar.*;
[error] ^
[error] 1 error
[error] (compile:compile) javac returned nonzero exit code

The two errors have very distinct signatures, and this combined with dependencies as mentioned above should help guide you to the right place.

Steve Chaloner
  • 8,162
  • 1
  • 22
  • 38