Is it possible to compile Java11 code to Java8 bytecode and run on 8?












7















I'm playing with some dependencies and compilation to older releases using java 11. I migrated one dependency to Java 11 and works fine, but we still have to run it Tomcat 7 or 8 on Java8. Is it possible to use the --release flag to compile code which uses var, stream().dropwhile(...) or Map.of(...) and run on 8?



Release flag suggest that it should be possible:




--release release Compiles against the public, supported and documented API for a specific VM version. Supported release targets
are 6, 7, 8, and 9.




This project is a dependency, stand-alone works fine with SprinBoot2.1 and Java11, but needs to be run in Java8.



My maven plugin compiler settings:



<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>8</release>
</configuration>
</plugin>


but this forbids compiling >jdk8 specific code. I'm using latest maven 3.6.0 and mvn compiler as above.



Attempt to compile:



return List.of("dsadas", "dasdadddds", "£dsada", "dasdas")
.stream()
.dropWhile(s -> s.contains("das"))
.collect(Collectors.toList());


throws error:



[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project api: Compilation failure: Compilation failure: 
[ERROR] /home/agilob/Projects/.....java:[58,13] cannot find symbol
[ERROR] symbol: class var
[ERROR] location:
[ERROR] /home/agilob/Projects/....java:[43,20] cannot find symbol
[ERROR] symbol: method of(java.lang.String,java.lang.String,java.lang.String,java.lang.String)
[ERROR] location: interface java.util.List
[ERROR] -> [Help 1]









share|improve this question

























  • Part of the problem seems to be that the static factory List.of was added in Java 9, and is therefore not present in 8’s runtime library.

    – MTCoster
    Nov 15 '18 at 18:14











  • Also the method dropWhile of java.base.Stream was added in Java 9 so it is not part of the supported API. See docs.oracle.com/javase/9/docs/api/java/util/stream/Stream.html

    – lalo
    Nov 15 '18 at 18:17













  • Yes, and var was added in Java10, but as these are compile time cosmetic changes to the language, couldn't they be statically linked and bundled in the .class and .jar?

    – agilob
    Nov 15 '18 at 18:17











  • @agilob in theory, yes. In practice I don't know of any tool which tries to do this. If you find one, let me know.

    – Peter Lawrey
    Nov 15 '18 at 19:03
















7















I'm playing with some dependencies and compilation to older releases using java 11. I migrated one dependency to Java 11 and works fine, but we still have to run it Tomcat 7 or 8 on Java8. Is it possible to use the --release flag to compile code which uses var, stream().dropwhile(...) or Map.of(...) and run on 8?



Release flag suggest that it should be possible:




--release release Compiles against the public, supported and documented API for a specific VM version. Supported release targets
are 6, 7, 8, and 9.




This project is a dependency, stand-alone works fine with SprinBoot2.1 and Java11, but needs to be run in Java8.



My maven plugin compiler settings:



<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>8</release>
</configuration>
</plugin>


but this forbids compiling >jdk8 specific code. I'm using latest maven 3.6.0 and mvn compiler as above.



Attempt to compile:



return List.of("dsadas", "dasdadddds", "£dsada", "dasdas")
.stream()
.dropWhile(s -> s.contains("das"))
.collect(Collectors.toList());


throws error:



[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project api: Compilation failure: Compilation failure: 
[ERROR] /home/agilob/Projects/.....java:[58,13] cannot find symbol
[ERROR] symbol: class var
[ERROR] location:
[ERROR] /home/agilob/Projects/....java:[43,20] cannot find symbol
[ERROR] symbol: method of(java.lang.String,java.lang.String,java.lang.String,java.lang.String)
[ERROR] location: interface java.util.List
[ERROR] -> [Help 1]









share|improve this question

























  • Part of the problem seems to be that the static factory List.of was added in Java 9, and is therefore not present in 8’s runtime library.

    – MTCoster
    Nov 15 '18 at 18:14











  • Also the method dropWhile of java.base.Stream was added in Java 9 so it is not part of the supported API. See docs.oracle.com/javase/9/docs/api/java/util/stream/Stream.html

    – lalo
    Nov 15 '18 at 18:17













  • Yes, and var was added in Java10, but as these are compile time cosmetic changes to the language, couldn't they be statically linked and bundled in the .class and .jar?

    – agilob
    Nov 15 '18 at 18:17











  • @agilob in theory, yes. In practice I don't know of any tool which tries to do this. If you find one, let me know.

    – Peter Lawrey
    Nov 15 '18 at 19:03














7












7








7








I'm playing with some dependencies and compilation to older releases using java 11. I migrated one dependency to Java 11 and works fine, but we still have to run it Tomcat 7 or 8 on Java8. Is it possible to use the --release flag to compile code which uses var, stream().dropwhile(...) or Map.of(...) and run on 8?



Release flag suggest that it should be possible:




--release release Compiles against the public, supported and documented API for a specific VM version. Supported release targets
are 6, 7, 8, and 9.




This project is a dependency, stand-alone works fine with SprinBoot2.1 and Java11, but needs to be run in Java8.



My maven plugin compiler settings:



<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>8</release>
</configuration>
</plugin>


but this forbids compiling >jdk8 specific code. I'm using latest maven 3.6.0 and mvn compiler as above.



Attempt to compile:



return List.of("dsadas", "dasdadddds", "£dsada", "dasdas")
.stream()
.dropWhile(s -> s.contains("das"))
.collect(Collectors.toList());


throws error:



[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project api: Compilation failure: Compilation failure: 
[ERROR] /home/agilob/Projects/.....java:[58,13] cannot find symbol
[ERROR] symbol: class var
[ERROR] location:
[ERROR] /home/agilob/Projects/....java:[43,20] cannot find symbol
[ERROR] symbol: method of(java.lang.String,java.lang.String,java.lang.String,java.lang.String)
[ERROR] location: interface java.util.List
[ERROR] -> [Help 1]









share|improve this question
















I'm playing with some dependencies and compilation to older releases using java 11. I migrated one dependency to Java 11 and works fine, but we still have to run it Tomcat 7 or 8 on Java8. Is it possible to use the --release flag to compile code which uses var, stream().dropwhile(...) or Map.of(...) and run on 8?



Release flag suggest that it should be possible:




--release release Compiles against the public, supported and documented API for a specific VM version. Supported release targets
are 6, 7, 8, and 9.




This project is a dependency, stand-alone works fine with SprinBoot2.1 and Java11, but needs to be run in Java8.



My maven plugin compiler settings:



<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>8</release>
</configuration>
</plugin>


but this forbids compiling >jdk8 specific code. I'm using latest maven 3.6.0 and mvn compiler as above.



Attempt to compile:



return List.of("dsadas", "dasdadddds", "£dsada", "dasdas")
.stream()
.dropWhile(s -> s.contains("das"))
.collect(Collectors.toList());


throws error:



[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project api: Compilation failure: Compilation failure: 
[ERROR] /home/agilob/Projects/.....java:[58,13] cannot find symbol
[ERROR] symbol: class var
[ERROR] location:
[ERROR] /home/agilob/Projects/....java:[43,20] cannot find symbol
[ERROR] symbol: method of(java.lang.String,java.lang.String,java.lang.String,java.lang.String)
[ERROR] location: interface java.util.List
[ERROR] -> [Help 1]






java maven






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 18:07









Mark

3,70921126




3,70921126










asked Nov 15 '18 at 17:57









agilobagilob

4,18411930




4,18411930













  • Part of the problem seems to be that the static factory List.of was added in Java 9, and is therefore not present in 8’s runtime library.

    – MTCoster
    Nov 15 '18 at 18:14











  • Also the method dropWhile of java.base.Stream was added in Java 9 so it is not part of the supported API. See docs.oracle.com/javase/9/docs/api/java/util/stream/Stream.html

    – lalo
    Nov 15 '18 at 18:17













  • Yes, and var was added in Java10, but as these are compile time cosmetic changes to the language, couldn't they be statically linked and bundled in the .class and .jar?

    – agilob
    Nov 15 '18 at 18:17











  • @agilob in theory, yes. In practice I don't know of any tool which tries to do this. If you find one, let me know.

    – Peter Lawrey
    Nov 15 '18 at 19:03



















  • Part of the problem seems to be that the static factory List.of was added in Java 9, and is therefore not present in 8’s runtime library.

    – MTCoster
    Nov 15 '18 at 18:14











  • Also the method dropWhile of java.base.Stream was added in Java 9 so it is not part of the supported API. See docs.oracle.com/javase/9/docs/api/java/util/stream/Stream.html

    – lalo
    Nov 15 '18 at 18:17













  • Yes, and var was added in Java10, but as these are compile time cosmetic changes to the language, couldn't they be statically linked and bundled in the .class and .jar?

    – agilob
    Nov 15 '18 at 18:17











  • @agilob in theory, yes. In practice I don't know of any tool which tries to do this. If you find one, let me know.

    – Peter Lawrey
    Nov 15 '18 at 19:03

















Part of the problem seems to be that the static factory List.of was added in Java 9, and is therefore not present in 8’s runtime library.

– MTCoster
Nov 15 '18 at 18:14





Part of the problem seems to be that the static factory List.of was added in Java 9, and is therefore not present in 8’s runtime library.

– MTCoster
Nov 15 '18 at 18:14













Also the method dropWhile of java.base.Stream was added in Java 9 so it is not part of the supported API. See docs.oracle.com/javase/9/docs/api/java/util/stream/Stream.html

– lalo
Nov 15 '18 at 18:17







Also the method dropWhile of java.base.Stream was added in Java 9 so it is not part of the supported API. See docs.oracle.com/javase/9/docs/api/java/util/stream/Stream.html

– lalo
Nov 15 '18 at 18:17















Yes, and var was added in Java10, but as these are compile time cosmetic changes to the language, couldn't they be statically linked and bundled in the .class and .jar?

– agilob
Nov 15 '18 at 18:17





Yes, and var was added in Java10, but as these are compile time cosmetic changes to the language, couldn't they be statically linked and bundled in the .class and .jar?

– agilob
Nov 15 '18 at 18:17













@agilob in theory, yes. In practice I don't know of any tool which tries to do this. If you find one, let me know.

– Peter Lawrey
Nov 15 '18 at 19:03





@agilob in theory, yes. In practice I don't know of any tool which tries to do this. If you find one, let me know.

– Peter Lawrey
Nov 15 '18 at 19:03












1 Answer
1






active

oldest

votes


















5














It is not possible. Only the API methods and classes of the targeted Java platform are allowed.



That is defined in JEP 247: Compile for Older Platform Versions




A new command-line option, --release, is defined, which automatically
configures the compiler to produce class files that will link against
an implementation of the given platform version.




The relevant part that says what can be used is:




For N < 9, the documented APIs consist of the public APIs that were on javac's default bootclasspath for JDK N.



For N >= 9, the documented APIs consist of (i) the APIs exported from those modules in the JDK image which are part of the documentation of JDK N; and (ii) the API exported from the jdk.unsupported module (documented in JEP 260).







share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325376%2fis-it-possible-to-compile-java11-code-to-java8-bytecode-and-run-on-8%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    5














    It is not possible. Only the API methods and classes of the targeted Java platform are allowed.



    That is defined in JEP 247: Compile for Older Platform Versions




    A new command-line option, --release, is defined, which automatically
    configures the compiler to produce class files that will link against
    an implementation of the given platform version.




    The relevant part that says what can be used is:




    For N < 9, the documented APIs consist of the public APIs that were on javac's default bootclasspath for JDK N.



    For N >= 9, the documented APIs consist of (i) the APIs exported from those modules in the JDK image which are part of the documentation of JDK N; and (ii) the API exported from the jdk.unsupported module (documented in JEP 260).







    share|improve this answer




























      5














      It is not possible. Only the API methods and classes of the targeted Java platform are allowed.



      That is defined in JEP 247: Compile for Older Platform Versions




      A new command-line option, --release, is defined, which automatically
      configures the compiler to produce class files that will link against
      an implementation of the given platform version.




      The relevant part that says what can be used is:




      For N < 9, the documented APIs consist of the public APIs that were on javac's default bootclasspath for JDK N.



      For N >= 9, the documented APIs consist of (i) the APIs exported from those modules in the JDK image which are part of the documentation of JDK N; and (ii) the API exported from the jdk.unsupported module (documented in JEP 260).







      share|improve this answer


























        5












        5








        5







        It is not possible. Only the API methods and classes of the targeted Java platform are allowed.



        That is defined in JEP 247: Compile for Older Platform Versions




        A new command-line option, --release, is defined, which automatically
        configures the compiler to produce class files that will link against
        an implementation of the given platform version.




        The relevant part that says what can be used is:




        For N < 9, the documented APIs consist of the public APIs that were on javac's default bootclasspath for JDK N.



        For N >= 9, the documented APIs consist of (i) the APIs exported from those modules in the JDK image which are part of the documentation of JDK N; and (ii) the API exported from the jdk.unsupported module (documented in JEP 260).







        share|improve this answer













        It is not possible. Only the API methods and classes of the targeted Java platform are allowed.



        That is defined in JEP 247: Compile for Older Platform Versions




        A new command-line option, --release, is defined, which automatically
        configures the compiler to produce class files that will link against
        an implementation of the given platform version.




        The relevant part that says what can be used is:




        For N < 9, the documented APIs consist of the public APIs that were on javac's default bootclasspath for JDK N.



        For N >= 9, the documented APIs consist of (i) the APIs exported from those modules in the JDK image which are part of the documentation of JDK N; and (ii) the API exported from the jdk.unsupported module (documented in JEP 260).








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 18:47









        lalolalo

        185312




        185312
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325376%2fis-it-possible-to-compile-java11-code-to-java8-bytecode-and-run-on-8%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python