I happened to have a job to modify an application made with Java 8 to run with Java 6, so I think it's unusual, so I'll summarize it. I don't think I usually do this ... (And I didn't want to do this kind of work)
I have a Restful Java 8 application that I was using in another project When I tried to apply the application to my project The server where the application is placed is called Red Hat Enterprise Linux 5.1 ...
I wanted you to upgrade the OS of the server where you want to deploy it, but it's unreasonable because other applications are running ...
That's why I decided to downgrade from Java8 to Java6.
By the way, according to Wiki, Red Hat Enterprise Linux 5.1 is 10 years old released on November 7, 2007. The standard support deadline is March 31, 2017, and the standard support deadline has already expired. Extended support is until November 30, 2020 ...
Here are some typical features added in Java 8.
In addition to these, many functions have been added. See the Release Notes for details.
As you can see, there were many changes in API and description method in Java 8, and the change was very large as Java.
Java backporting tools Java backporting tools is a program for converting Java class files from one version to an older version. The following two were used this time.
Retrolambda
RetroLambda is a Maven, Gradle plugin
It converts the Java 8 description to a lower version when building.
Looking at the built class file, apart from XXX.class
, a class file such as XXX $$ Lambda $ 9.class
is output,
You can see that Java 8 descriptions such as Lambda expressions are processed as separate class files.
The following are supported by Retrolambda.
Almost all new descriptions are supported.
Here, I will explain how to use it in Maven. For Gradle, see ReadMe here.
Describe as follows in pom.xml.
<plugin>
<groupId>net.orfjackal.retrolambda</groupId>
<artifactId>retrolambda-maven-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<goals>
<goal>process-main</goal>
<goal>process-test</goal>
</goals>
</execution>
</executions>
<configuration>
<target>1.6</target>
</configuration>
</plugin>
Now it will be built automatically on the mvn life cycle.
configuration.target
describes the converted Java version.
When developing (settings such as IDE), develop using Java8 SDK and The built Jar is executed by Java6.
The problem with this method is that it can be compiled even if you are using the Java 8 API (of course you will get an error at runtime), There seems to be a plug-in that warns you of that. (I have not used it)
streamsupport covers many of the new Java 8 APIs. The ones covered are as follows.
pom.xml
Add the following to dependencies
in pom.xml.
(Streamsupport.version is defined in properties
.)
<!-- To use Java8 stream,functional API in Java6 -->
<dependency>
<groupId>net.sourceforge.streamsupport</groupId>
<artifactId>streamsupport</artifactId>
<version>${streamsupport.version}</version>
</dependency>
<dependency>
<groupId>net.sourceforge.streamsupport</groupId>
<artifactId>streamsupport-cfuture</artifactId>
<version>${streamsupport.version}</version>
</dependency>
<dependency>
<groupId>net.sourceforge.streamsupport</groupId>
<artifactId>streamsupport-flow</artifactId>
<version>${streamsupport.version}</version>
</dependency>
<dependency>
<groupId>net.sourceforge.streamsupport</groupId>
<artifactId>streamsupport-atomic</artifactId>
<version>${streamsupport.version}</version>
</dependency>
<dependency>
<groupId>net.sourceforge.streamsupport</groupId>
<artifactId>streamsupport-literal</artifactId>
<version>${streamsupport.version}</version>
</dependency>
Basically, it can be called with the same class name and the same method name with a package name different from the java8 API.
For example, the Completable Future API
import java.util.concurrent.CompletableFuture;
↓
import java8.util.concurrent.CompletableFuture;
become that way.
You can create a stream instance with RefStreams.of
.
import java8.util.stream.RefStreams;
RefStreams.of("a", "b", "c")
.map(String::toUpperCase)
.forEach(System.out::println);
In Java 8, stream method is added to List class as follows. In this case, it is as follows.
import java.util.Arrays;
import java.util.List;
List<String> list = Arrays.asList("a", "b", "c");
list.stream()
.map(String::toUpperCase)
.forEach(System.out::println);
↓
import java.util.Arrays;
import java.util.List;
import java8.util.stream.StreamSupport;
List<String> list = Arrays.asList("a", "b", "c");
StreamSupport.stream(list)
.map(String::toUpperCase)
.forEach(System.out::println);
I hope that readers of this will not have the opportunity to downgrade Java 8 applications to Java 6. .. ..
Recommended Posts