Which Jdk's Distributions Can Run 'Javac -Source 1.6 -Target 1.5'

Which JDK's distributions can run `javac -source 1.6 -target 1.5`?

According to the documentation (Java 5, Java 6), the Oracle SDK should be able to do this when you follow the instructions in the Cross-Compilation Example.

Java 6 should support any version between 1.3 to 1.6 as -target; it doesn't say anything what happens when you use generics and other "compatible" features in the source. The compiler should be able to strip them.

Another culprit in the game might be javac: The compiler might be able to handle this set of arguments but the command line tool might take offense.

In this case, write your own command line using the Java Compiler API. That might allow to pull of some tricks that you can't achieve otherwise.

You can also try the Eclipse compiler (see "Using the batch compiler").

This might fail because of how Java works: Java X code can run on Java Y as long as X <= Y. So while you can easily compile Java 1.4 code for a Java 6 VM, the reverse is not always true.

If everything else fails, write a preprocessor that reads the source and strips unsupported elements (like @Override on interfaces). As long as you compile the code with the annotations once in a while with Java 6, the converted code should be safe as well (unless your code stripper has a bug ...)

Maven: javac: source release 1.6 requires target release 1.6

The limitation is in javac. The solution is to tell maven to use another compiler. See question for details.

Many versions of JDK: How do I specify which one is used?

Two solutions:

  • Specify the full path in your command:
    for example /opt/java/jdk16/bin/javac ... on Linux
  • Use the -source and -target arguments of the javac command. This allows you specify the source code level and targeted JRE version

Also note:

  • Some Linux distributions can include tools to specify which JDK version to use by default.
  • Using -source and -target checkes that your language constructs are compliant with the targeted runtime, but does NOT check that core classes are compatible. This means that compiling with -source 1.4 on a JDK 1.6 will be just fine, even if you use String.isEmpty() which appeared in Java 6. This might lead to errors at runtime

How to write code in Java 11, but target Java 8 and above?

While conversion of classes compiled for JDK 11 to JDK 8 would be theoretically possible with a sophisticated tool, it’s not trivial. There are significant changes on the binary level.

First, JDK 11 introduced nest types, which eliminates the need to generate synthetic accessor methods when accessing private members of inner/outer classes. Of course, such access would fail in older versions.

It also introduced dynamic constants, though I don’t know whether the Java language exploits that feature anywhere. This is mainly intended for future versions.

Then, since JDK 9, string concatenation gets compiled using invokedynamic referring to java.lang.invoke.StringConcatFactory which is not present in Java 8.

A feature that could work, is private methods in interfaces, introduced in Java 9 as a language feature, but already handled on the binary level in Java 8.

Java 8 would also be unable to process module definitions, but I suppose, they would be ignored.

Ant javac difference between target and source attributes?

ANT is itself a Java program, so it uses the JAVA_HOME environment variable to choose which JVM to use at runtime.

Some ANT tasks allow you to choose a different Java compiler or JRE, over-riding the default option. For more details see the ANT documentation:

  • javac
  • java

Update

I have Java 6 and 7 installed. How do I specify ant to use Java 6 to compile?

The sources and target attributes of the javac task control compatibility settings. they do not control which JDK is used by ANT.

They enable a modern JDK to compile older versions of the Java programming language and/or produce bytecode compatible with older versions of the Java runtime.

sources is part of the standard options and targets is detailed under the cross compilation section of the javac command's documentation from Oracle:

  • http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/javac.html

Finally, Oracle have produced the following document on Java compatibility:

  • http://www.oracle.com/technetwork/java/javase/compatibility-417013.html

It states that Java 6 and Java 7 are strongly compatible, which was not always the case in previous versions of Java.

PS
Apologies to repeating the same links, but hopefully the concepts are clearer?

Why does Ant compile Java source that has undefined methods for its target Java version

You can compile code of higher version f.ex. 1.8 with compatability or target version a lower version for example 1.6. This will translate the code to byte code compatible with the corresponding version. The reason for this is to be capable of pruducing artifacts compatible with alternative java versions.

This is a built in java function and has nothing to do with ant. Here is equivalent java compiler intruction:

javac -source 1.7 -target 1.5 MyTest.java

The -source defines a minimum version of Java that needs tro be capable of compaling the source code. The condition when using Java 1.8 is met. When using Java 7 the condition is again met, so compilation is attempted , but it eventualy fail because of the Java 8 feature usage.

If you had Java 1.6 then the compilator would have failed imediatly without attempting to perform compilation.

Why won't Eclipse compile my code in java 1.5?


see the compiler error output for details.

You probably have a dependency on a library that was compiled using a later version of Java than your 1.5 JDK.

Actually, where is your 1.5 JDK? All I see is a JRE. My guess is that you just need to download a version 1.5 JDK and add that in Eclipse.

javac: target release 7 conflicts with default source release 1.8

add a -source that matches your -target, so 1.7 I believe. Should fix it.

Otherwise you are compiling all those cool 1.8 closures onto a 1.7 jar to run on a 1.7 jvm which is just not going to work.



Related Topics



Leave a reply



Submit