-1

I am running a Jetty web application using IBM JDK 7 which throws the following exception even after setting java.util.Arrays.useLegacyMergeSort=true java.lang.IllegalArgumentException: Comparison method violates its general contract!

I have added the following line of code to test that the property is set correctly and it is. System.getProperty("java.util.Arrays.useLegacyMergeSort") returns true

Any idea why when the property is set correctly to use the legacy merge sort that I still get the java.lang.IllegalArgumentException?

Shane
  • 26
  • 1
  • 1

2 Answers2

2

It's possible that you're simply not setting the System property early enough. You may need to pass it as a JVM parameter (e.g. using -Djava.util.Arrays.useLegacyMergeSort=true on the command line) rather than calling System.setProperty() if you want to ensure it is set before the Arrays class is initialized (see also this answer).

To the best my my knowledge, useLegacyMergeSort is not a Java standard, and so it is not required to be implemented by any specific JVM implementation. So even if you can get it to work now, there is no guarantee that a future version of Java will continue to support it.

As Louis Wasserman says in the comment above, though, you shouldn't need to set this flag in the first place. This is a workaround for broken code. The solution is not to find a new workaround, it's to fix the broken code so that the workaround is no longer necessary.

Community
  • 1
  • 1
Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135
1

Why? Without a deep analysis of your code, it is difficult to be sure, but one possibility is that you set the property too late; e.g. you set it in the code rather than via a -D command line option.

Another possibility is that the "violates contract" exception is being thrown because of another problem that the one that flag addresses. That flag is a workaround for comparators that don't deal with the "equal" case. It could be that your comparator violates the contract in other ways.

As Louis Wasserman notes, the best solution is to fix the comparator, instead of attempting (vainly it seems) to work around the bug in the comparator with dubious compatibility flags.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216