Maven: javac: source release 1.6 requires target release 1.6

后端 未结 6 1061
说谎
说谎 2020-12-08 04:48

NOTE: This appears to be a limit in the \"javac\" program.

I have Java 6 code that needs to be built for a Java 5 JVM. My previous work with the javac ant target (

相关标签:
6条回答
  • 2020-12-08 05:31

    What Java 6 language features are you using that are not present in Java 5? As far as I can tell, the only "feature" that's been added to the language is the use of the @Override annotation in interfaces. Otherwise, Java 6 and Java 5 are source-compatible. What happens when you use:

    <source>1.5</source>
    <target>1.5</target>
    

    in your Maven build file?

    0 讨论(0)
  • 2020-12-08 05:31

    I had the same error when I upgraded my IntelliJ IDE, it was fixed with the replacement of 1.5 with 1.6 as below.

           <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
    
    0 讨论(0)
  • 2020-12-08 05:47

    I believe you need to set -bootclasspath as well so that javac compiles against JDK 1.5 bootstrap classes.

    Try:

    javac -source 1.6 -target 1.5 -bootclasspath /path/to/jdk1.5/lib/rt.jar -extdirs "" Foo.java
    

    UPDATE:

    Try removing the -source option, but keep the -target option.

    I just tested it out:

    # no source, only target => COMPILES to 1.5
    $ javac -target 1.5 Foo.java
    $ javap -v  Foo | grep version
      minor version: 0
      major version: 49
    
    # no source, no target => COMPILES to 1.6
    $ javac Foo.java
    $ javap -v  Foo | grep version
      minor version: 0
      major version: 50
    
    # both source and target => ERROR
    $ javac -source 1.6 -target 1.5 Foo.java
    javac: source release 1.6 requires target release 1.6
    
    $ javac -version
    javac 1.6.0_21
    
    0 讨论(0)
  • 2020-12-08 05:47

    The real question is thus to use @Override in source files that need to be compiled in Java 1.5 class files.

    javac -source 1.5 -target 1.5 aFileWithOverride.java
    

    will do just that. In jdk 7, this will lead to a warning

    [options] bootstrap class path not set in conjunction with -source 1.5
    

    which is absent in jdk 6.

    To get rid of this warning a special boot class jar can be created by adding the java 6 (or higher) java.lang.annotation package to a java 5 rt.jar.

    0 讨论(0)
  • 2020-12-08 05:49

    It seems if you want to do cross compilation you need to supply a couple of extra arguments -bootclasspath and -extdirs, although I believe you only need the first. For using Javac and example can be found here with an explanation of the additional options here (Cross-Compilation Options section).

    You would then need to configure these options for your maven-compiler-plugin. From what I understand you need to set to plugin to fork so that it will use the compiler arguments rather than the built in compiler. You can find a listing of all the options here

     <build>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.6</source>
                        <target>1.5</target>
                        <fork>true</fork>
                        <compilerArguments>
                            <bootclasspath>${1.5.0jdk}\lib\rt.jar</bootclasspath>
                       </compilerArguments>
                   </configuration>
               </plugin>
            ....
           </plugins>
       </build>
    
    0 讨论(0)
  • 2020-12-08 05:50

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

    0 讨论(0)
提交回复
热议问题