Can I use a jar, compiled in Java 7 as a dependency in a project that is compiled against Java 6?

会有一股神秘感。 提交于 2020-03-22 06:21:56

问题


Can I use a jar, compiled in Java 7 as a dependency in a project that is compiled for compatibility with Java 6? I do not have the source code of the dependency. I have the compiled jar as a maven dependency.


回答1:


Can I use a jar, compiled in Java 7 as a dependency in a project that is compiled for compatibility with Java 6?

Let's unpick this:

  • You have a project that is compiled so that will run on a Java 6 JRE. (Lets suppose that you only use Java 6 APIs in that project.) The .class files for this project must have a classfile format major version of 50 ... otherwise a Java 6 JRE won't be able to load them.

  • Then you have a dependency that is "compiled in Java 7". That could mean one of two things:

    1. It could have been compiled using a Java 7 tool chain but with a target version of Java 6.

    2. It could have been compiled using a Java 7 tool chain for Java 7.

In both subcases above above, you should be able to use the dependency in your Java 6 project if you run the project on a Java 7 JRE1. A Java 7 JRE can load and run classfiles compiled for Java 6. In one of the subcases, you will be loading classes with two (or more) class version numbers. But that is OK.

On the other hand, if you try to run the code on a Java 6 JRE, then:

  • Subcase 1 will work provided that the Java 7 dependency doesn't make use of any Java 7 (or later) APIs; i.e. it only uses Java standard classes, methods, etc that were present in Java 6 or earlier.

  • Subcase 2 will not work. The Java 6 JRE won't be able to load the dependency. Indeed, if the dependency is static (i.e. the project source code has compile time dependencies on the APIs of the dependent), then the project code won't build ... because the Java 6 compiler should refuse to read the dependency's newer version classfiles.


The most advisable approach is to migrate your project and your execution platform to Java 7. Or better still to Java 9, since Java 7 is EOL'd.

If you can't do that, the next best thing would be to avoid using the Java 7 dependency ... until you can upgrade.

If you have customers who insist they you continue to support Java 6, then they are impeding your ability to progress your product line. They should be charged a premium for that.

If you are avoiding upgrading your Java platform for internal reasons, this decision is accumulating technical dept ... that your organization will be forced to pay off in the long term.


1 - .... or JDK. A JDK is equivalent to a JRE for the purposes of running code.




回答2:


In your case you actually ask if there is Forward Compatibility between Java 6 and Java 7. Generally speaking Java does not support Forward Compatibility as the 1.7 JVM cannot run code compiled with 1.6. This happens mainly because the version of 1.7 compiled Java bytecode is not known by the older version (1.6).



来源:https://stackoverflow.com/questions/49551633/can-i-use-a-jar-compiled-in-java-7-as-a-dependency-in-a-project-that-is-compile

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