pythonic
pythonic

Reputation: 21635

How to generate a jar file that is able to run on Java 1.7, with sbt and Java 1.8

I have to compile a Spark program which runs with Java 1.7. However, I have Java 1.8 installed on my computer. My code has both Scala and Java classes, and I compile using sbt. I know you that one can set target with javac, but since I compile using sbt, and therefore don't directly call javac, how can I compile such code for Java 1.7, with Java 1.8.

P.S: I don't want to separately install Java 1.7 on my computer.

Upvotes: 3

Views: 351

Answers (1)

T. Gawęda
T. Gawęda

Reputation: 16086

Java is backward compatible, which means you can compile to Java 7 bytecode using Java 8 compiler

In sbt you must manually set target Java version:

javacOptions ++= Seq("-source", "1.7", "-target", "1.7"),
scalacOptions := Seq("-target:jvm-1.7")

It will tell javac and scalac to set Java 7 class headers

References: first and second

Upvotes: 4

Related Questions