且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我可以在Java 7中编译的项目中使用Java 7中编译的jar作为依赖项吗?

更新时间:2022-12-12 20:47:12

我是否可以在Java 7中将jar编译为与Java 6兼容的项目中的依赖项,而jar是在Java 7中编译的?

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

让我们取消选择:

  • 您有一个已编译的项目,因此可以在Java 6 JRE上运行. (假设您仅在该项目中使用Java 6 API.)此项目的.class文件必须的类文件格式主版本为50 ...否则,将不会使用Java 6 JRE能够加载它们.

  • 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.

然后,您具有一个用Java 7编译"的依赖项.这可能意味着两件事之一:

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

  1. 它可以使用Java 7工具链进行编译,但可以使用Java 6的目标版本.

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

它可以使用Java 7的Java 7工具链进行编译.

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

在上述两个子情况下,如果在Java 7 JRE 1 上运行项目,则应该在Java 6项目中使用依赖项. Java 7 JRE可以加载和运行针对Java 6编译的类文件.在其中一种情况下,您将加载具有两个(或更多)类版本号的类.但这没关系.

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.

另一方面,如果您尝试在Java 6 JRE上运行代码,则:

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

  • 第1种情况将工作,前提是Java 7依赖项不使用任何Java 7(或更高版本)API;即,它仅使用Java 6或更早版本中存在的Java标准类,方法等.

  • 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.

子案例2将不起作用. Java 6 JRE将无法加载依赖项.确实,如果依赖项是静态的(即项目源代码具有依赖项的API的编译时依赖项),则该项目代码将无法构建……因为Java 6编译器应拒绝读取依赖项的较新版本的类文件.

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.

最可取的方法是将您的项目和执行平台迁移到Java7.或者,***将其迁移到Java 9,因为Java 7已停止运行.

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.

如果您无法做到这一点,那么下一个***的办法就是避免使用Java 7依赖项...直到可以升级为止.

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

如果您的客户坚持要求您继续支持Java 6,那么他们会阻碍您升级产品线的能力.应该为此向他们收取额外费用.

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.

如果您出于内部原因而避免升级Java平台,则此决定正在累积技术部门...您的组织将在长期内***获得回报.

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-....或JDK.就运行代码而言,JDK等效于JRE.