Maven: javac: source release 1.6 requires target release 1.6

て烟熏妆下的殇ゞ 提交于 2019-11-28 04:58:34

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

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>

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

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?

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

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!