How to customize the lower language level in Java

I maintain a Java tutorial application written in Java that demonstrates the features of Java 6, 7, and 8. Each tutorial screen is loaded dynamically by Class.forName as needed. Java 7 classes must use Java 7 constructors, and Java 8 classes must use Lambdas and the Date / Time API.

I would like this to be available to users of all three levels of the language, only if they are on Java 6, we will not load Java 7 or 8 classes, and if on 7 we will not load Java 8 classes.

We will dynamically determine the runtime level and ignore the higher levels, loading only the appropriate Class.forName () classes. Obviously, users with Java 6 or 7 will not be able to execute code with the magic number 8. So I would like to target an earlier version of Java in an assembly.

In other words, I would like to set source to 1.8 and target to 1.6

However Maven (and Javac) do not allow me to specify the target below the source.

Is there another way?

I've done this in the past with JavaFX code for Android where I was able to generate JavaFX 8 code using Lambdas and then deploy it to the current Android version. But in this case, there was a special SDK that handled lambdas and built on Java 7.

Is there a way to do this with standard Java 8?

+3


source to share


2 answers


The only option you have is -source 1.1 -target 1.0 In every other case they should be the same



You need to create a jar for each version and another that combines them.

+1


source


You will need to make 3 different jars, one for each version, where the later version includes more of your code examples. (on the bright side, you just need to recompile your java 6 and 7 code a few times)



for each bank, set the code -target

to 6, 7 or 8, respectively. You can also use a parameter -source

and set it to the same value, but I don't think you need to, unless you're worried about accidentally including later language features.

+1


source







All Articles