Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

javac

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?

like image 652
Alex Avatar asked Apr 09 '13 04:04

Alex


1 Answers

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();
    }
like image 191
Evgeniy Dorofeev Avatar answered Nov 05 '22 22:11

Evgeniy Dorofeev