Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sbt: compile using Java 6 and run using Java 7

Tags:

jvm

scala

sbt

I have a Scala 2.10.3 project that uses Swing. I have the following catch-22 situation:

  • I must compile against Java 6 because of a bug with Scala Swing not working with Java 7
  • I must run the project with Java 7 because OpenJDK 6 on Linux is broken (crashes with various Swing components)

I can compile with

$ sbt -java-home /usr/lib/jvm/java-6-openjdk-amd64/ test:products

But when I try to run:

$ sbt run

sbt figures that the JVM version changes, and tries to recompile everything, thus throwing a compile error due to the first problem.

How can I convince sbt to run my project that was already compiled, although using a different JVM? Using assembly is not an option, because that takes several minutes, and I need to do this a lot.


I also tried to switch using export JAVA_HOME, but this has the same effect, sbt will try to recompile upon run.

like image 312
0__ Avatar asked Mar 20 '23 12:03

0__


1 Answers

You can use the javaHome key, scoped to the run task, to control the JDK used when running.

Assuming you run SBT with JDK6, as in:

sbt -java-home /usr/lib/jvm/java-6-openjdk-amd64/

Add a custom location for javaHome in your build.sbt:

// no custom Java_HOME without forking
fork in run := true

// your JDK7 install
javaHome in run := Some(file("/usr/lib/jvm/java-7-openjdk-amd64/"))

compile will then use JDK6, and run JDK7. You can also remove in run in the above definitions to have it apply to both run and test.

See the Forking section of the SBT documentation for more details.

like image 169
gourlaysama Avatar answered Mar 27 '23 15:03

gourlaysama