Java Sources


sbt has support for compiling Java sources in sbt with the following limitations:

  • The compiler used is the javac used to run sbt or the javac on your path.
  • Dependency tracking is limited to the dependencies present in compiled class files.


  • compile will compile the sources under src/main/java
  • test-compile will compile the sources under src/test/java.

Pass options to the Java compiler by adding instances of JavaCompileOption to your compileOptions. For example:

  override def compileOptions = javaCompileOptions("-g:none") ++ super.compileOptions.toList

As with options for the Scala compiler, the arguments are not parsed by sbt. Multi-element options, such as -source 1.5, are specified like:

  override def javaCompileOptions = super.javaCompileOptions ++
    javaCompileOptions("-source", "1.5")

You can specify the order in which Scala and Java sources are built with the compileOrder method. Possible values are from the CompileOrder enumeration: Mixed, JavaThenScala, and ScalaThenJava. If you have circular dependencies between Scala and Java sources, you need the default, Mixed, which passes both Java and Scala sources to scalac and then compiles the Java sources with javac. If you do not have circular dependencies, you can use one of the other two options to speed up your build by not passing the Java sources to scalac. For example, if your Scala sources depend on your Java sources, but your Java sources do not depend on your Scala sources, you can do:

  override def compileOrder = CompileOrder.JavaThenScala