Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does there exist a Babel like compiler for Java?

With javascript if we want to make sure that our code runs in all browser versions we can use Babel. Is there something like this for Java, where we could write our code in Java 9, but it will run in a Java 6 runtime?

For example can Kotlin target multiple JVM runtime versions?

like image 707
Ole Avatar asked Oct 20 '17 22:10

Ole


3 Answers

I was hoping for something like Kotlin to target multiple JVM runtimes - I guess we just have to dream for now.

You can compile Kotlin code to JDK6, JDK7, JDK8, JDK9 or any JDK above JDK6. This is what meant by supporting Java 1.6 level byte code. All features of Kotlin will stay the same, except for libraries, which can require different JDK versions.

The byte code generated by Kotlin will generally stay the same independent of the target JVM version. An exception is if you set a compiler option jvmTarget = "1.8", then the compiler may (or may not) use some features of JDK8 as an optimization.

IMHO this question got all the minuses because of how unexpected it is. Tools like Babel are unique to JavaScript because in all other languages they are called compilers. Since JS decided it could do without a compiler, I has such problems with deployments. There are (very limited) back porting tools for Java, but they are just plugins to the compiler. Kotlin doesn't have any, because its development is independent of JDK and it has to support all previous JDK versions above 1.6.

To sum it up, if you use Kotlin for JVM or JS development, your dream have come true - you can use any version of Kotlin, with any JVM library, probably any JS library above ES5.1, and get consistent runtime representation.

like image 191
voddan Avatar answered Nov 20 '22 11:11

voddan


There is an unofficial library retrolambda which compiles Java 8 feature lambda expression into Java 6(just like Babel).

I guess you will enjoy it, and here it is: https://github.com/orfjackal/retrolambda

like image 28
fashare2015 Avatar answered Nov 20 '22 12:11

fashare2015


You can check teaVM ! http://teavm.org/ also there is dukeScript

like image 38
JOKe Avatar answered Nov 20 '22 11:11

JOKe