Run an application made with Java8 with Java6

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)

background

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 ...

Specification changes and additions in Java 8

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.

How to use

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

streamsupport covers many of the new Java 8 APIs. The ones covered are as follows.

How to use

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>

Basic usage

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.

How to use Stream API

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);

Impressions

I hope that readers of this will not have the opportunity to downgrade Java 8 applications to Java 6. .. ..

Recommended Posts

Run an application made with Java8 with Java6
Run an application made with Go on Heroku
Run batch with docker-compose with Java batch
Run Java VM with WebAssembly
[Tutorial] Download Eclipse → Run Web application with Java (Pleiades)
I made an Android application that GETs with HTTP
Create an immutable class with JAVA
About creating an application with springboot
I made an annotation in Java.
Run an external process in Java
I tried to make an Android application with MVC now (Java)
[Java] Create an executable module with Gradle
One-JAR Java EE application with WebSphere Liberty
I made an eco server with scala
Java development with Codenvy: Hello World! Run
Vending machine made with Java (domain driven)
Run WEB application with Spring Boot + Thymeleaf
Java 9+ application status
Run Rust from Java with JNA (Java Native Access)
Refactored GUI tools made with Java8 + JavaFX in 2016
Simple obstacle racing made with processing for Java
Build an E2E test environment with Selenium (Java)
CICS-Run Java application-(1) Run a simple sample app
[Java] Find prime numbers with an Eratosthenes sieve
Get along with Java containers in Cloud Run
Create an ARM-cpu environment with qemu on mac and run java [Result → Failure]
Install java with Homebrew
Trace the SQL executed with the application Insights java agent
I made an iPhone Theremin with Vision framework + AudioKit
[Java] Find prime numbers with an Eratosthenes sieve (Part 2)
Try running an app made with Quarkus on Heroku
Install Java with Ansible
Experienced Java users get started with Android application development
[java] throw an exception
Make Calendar gadgets made with JavaFX compatible with Java SE 9
Run Pico with docker
Run PostgreSQL on Java
Comfortable download with JAVA
Switch java with direnv
Quickly implement a singleton with an enum in Java
Download Java with Ansible
Let's scrape with Java! !!
Face recognition app made with Amazon Rekognition and Java
Build Java with Wercker
[Rails] Create an application
Serverless Java EE starting with Quarkus and Cloud Run
Endian conversion with JAVA
Deploy a Java application developed locally with the Cloud Toolkit to an Alibaba Cloud ECS instance
Story of making a task management application with swing, java
I made an interpreter (compiler?) With about 80 lines in Ruby.
Getting started with Java and creating an AsciiDoc editor with JavaFX
parquet-tools gives java.lang.ExceptionInInitializerError, so I made it work with java8
I want to push an app made with Rails 6 to GitHub
I made an adapter for communication class with Retrofit2 + Rx2
Run analysis with OpenJDK11 Java Flight Recorder + Google Kubernetes Engine
Java web application development environment construction with VS Code (struts2)
Try developing a containerized Java web application with Eclipse + Codewind
Java: Start WAS with Docker and deploy your own application
Create a simple DRUD application with Java + SpringBoot + Gradle + thymeleaf (1)
Easy BDD with (Java) Spectrum?
Use Lambda Layers with Java