1

I have code that uses Java 17 features and I am using JDK 17. Is it possible to make a JAR file with bytecode from this Java code that will be executable in 32b JRE 1.8?

Michal Špondr
  • 1,337
  • 2
  • 21
  • 44
  • 5
    No. You can instruct the compiler to generated bytecode for java 8, but then you can not use language features that were introduced after java 8. – f1sh Aug 23 '22 at 08:35
  • @f1sh What if I use JDK 17 with 1.8. Will the compiled JAR file be executable on 32b JRE 1.8? – Michal Špondr Aug 23 '22 at 11:57
  • Setting the target java version in the `pom.xml` is they way to go, yes. [Here](https://www.baeldung.com/maven-java-version) are some more details on that. But the problem remains: If you use language features of java9 and beyond, you project won't compile. – f1sh Aug 23 '22 at 12:19
  • @f1sh I assume Idea gives me warning if I use Java 17 features while using java.version 1.8. Thanks for the hint. Can you write it as a standard answer so I can accept it? – Michal Špondr Aug 23 '22 at 12:28

1 Answers1

3

Unfortunately, no.

You can instruct the compiler to generate bytecode for java 8, but then you can not use language features that were introduced after java 8. If you attempt to do so, your code will not compile.

Here are some details how to specify the target java version maven's pom.xml file.

Your only option is to re-write your code so that it compiles with java 8.

f1sh
  • 11,489
  • 3
  • 25
  • 51
  • Then why does `javac` have 2 flags, `-source` and `-target`? – Robert Aug 23 '22 at 15:39
  • @Robert because you can make the compiler treat it as e.g. java8 code but make it produce java17 bytecode which will have [class file version 61](https://stackoverflow.com/questions/9170832/list-of-java-class-file-format-major-version-numbers). – f1sh Aug 23 '22 at 17:36
  • So it only works in one direction, accept Java 8 sources and produce Java 17 class files, but not the other way around, accepting Java 17 sources and producing Java 8 compatible class files? – Robert Aug 23 '22 at 17:53