0__
0__

Reputation: 67300

Sbt: compile using Java 6 and run using Java 7

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

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.

Upvotes: 4

Views: 2428

Answers (2)

gourlaysama
gourlaysama

Reputation: 11290

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.

Upvotes: 7

0__
0__

Reputation: 67300

Here is one possible possible solution: The project is developed with IntelliJ IDEA.

  • IDEA runs on JDK 7 anyway
  • Use the third party sbt plug-in for IDEA.
  • In File -> Settings -> SBT (third party), locate "IDE Settings", check "Use alternative JRE" and select JDK 6 here.
  • Now the project can be compiled with the integrated sbt console, since sbt is launched using JDK 6
  • Run the project with a standard IDEA run configuration, using "Before launch: Run SBT Action 'test:products'". IDEA will launch the project using JDK 7

Upvotes: 0

Related Questions