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

萝らか妹 提交于 2019-12-03 15:37:39

问题


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?

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)

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

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.

Any suggestions?


回答1:


try to set system property

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

Note that it's not from Arrays public API but from 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();
    }


来源:https://stackoverflow.com/questions/15893487/compile-in-java-6-run-in-7-how-to-specify-uselegacymergesort

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