在 Java 6 中编译,在 7 中运行 - 如何指定 useLegacyMergeSort?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15893487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:12:20  来源:igfitidea点击:

Compile in Java 6, run in 7 - how to specify useLegacyMergeSort?

javajavac

提问by Alex

I'm wondering if I compile in Java 6, but someone runs the program on Java 7, will the Java 6 or 7 version of Arrays.sort be used?

我想知道我是否在 Java 6 中编译,但有人在 Java 7 上运行该程序,是否会使用 Java 6 或 7 版本的 Arrays.sort?

It's important because the new mergesort throws an IllegalArgumentException, and the old one doesn't (see Comparison method violates its general contract! Java 7 only)

这很重要,因为新的归并排序会抛出 IllegalArgumentException,而旧的则不会(请参阅比较方法违反其一般约定!仅限 Java 7

Now, it's possible to compile in Java 7 using Arrays.useLegacyMergeSort, but obviously that flag isn't available for Java 6 - and we want to be compatible on Mac OS Snow Leopard (which uses 6).

现在,可以使用 Arrays.useLegacyMergeSort 在 Java 7 中进行编译,但显然该标志不适用于 Java 6 - 我们希望与 Mac OS Snow Leopard(使用 6)兼容。

For some reason (see http://madbean.com/2006/target14/) the -target compiler flag doesn't seem to produce compatible code, so we'd rather compile in Java 6.

出于某种原因(请参阅http://madbean.com/2006/target14/) -target 编译器标志似乎无法生成兼容的代码,因此我们宁愿在 Java 6 中进行编译。

Any suggestions?

有什么建议?

回答by Evgeniy Dorofeev

try to set system property

尝试设置系统属性

java -Djava.util.Arrays.useLegacyMergeSort=true ...

java -Djava.util.Arrays.useLegacyMergeSort=true ...

Note that it's not from Arrays public API but from src

请注意,它不是来自 Arrays 公共 API,而是来自 src

   /**
     * Old merge sort implementation can be selected (for
     * compatibility with broken comparators) using a system property.
     * Cannot be a static boolean in the enclosing class due to
     * circular dependencies. To be removed in a future release.
     */
    static final class LegacyMergeSort {
        private static final boolean userRequested =
            java.security.AccessController.doPrivileged(
                new sun.security.action.GetBooleanAction(
                    "java.util.Arrays.useLegacyMergeSort")).booleanValue();
    }