How to remove Checkstyle info (wrong order for import org.apache.log4j.Logger)

25,302

Solution 1

ctrl+shift+o (organize imports) will make Eclipse order your imports correctly.

There is a convention according to which imports should be ordered, and checkstyle is telling you that you have not listed your imports in that order.

You can read more about it in the ImportOrder section of the documentation:

Checks the ordering/grouping of imports. Features are:

  • groups imports: ensures that groups of imports come in a specific order (e.g., java. comes first, javax. comes second, then everything else)
  • adds a separation between groups : ensures that a blank line sit between each group
  • sorts imports inside each group: ensures that imports within each group are in lexicographic order
  • sorts according to case: ensures that the comparison between imports is case sensitive
  • groups static imports: ensures the relative order between regular imports and static imports (see import orders)

Solution 2

You can also modify your check file to go by what eclipse does by default. You need to change the module "CustomImportOrder" and change "customImportOrderRules".

See http://checkstyle.sourceforge.net/config_imports.html#CustomImportOrder on how to customize it more.

This is what I am currently using:

<module name="CustomImportOrder">
    <property name="specialImportsRegExp" value="gov." />
    <property name="sortImportsInGroupAlphabetically" value="true" />
    <property name="customImportOrderRules"
        value="STATIC###SPECIAL_IMPORTS###STANDARD_JAVA_PACKAGE###THIRD_PARTY_PACKAGE" />
</module>

Solution 3

Look at Preferences > Java > Code Style > Organize Imports to configure the sort order and grouping that the Source > Organize Imports command uses (Ctrl+Shift+O, on OS X Cmd+Shift+O.

Share:
25,302

Related videos on Youtube

Saikat
Author by

Saikat

... and while writing the code visit StackOverflow ;)

Updated on August 16, 2020

Comments

  • Saikat
    Saikat almost 4 years

    I can see a Checkstyle info which says - Wrong order for import, org.apache.log4j.Logger. I could not get much info on why I am getting this. Any help would be appreciated. Below is the code snippet:

    import jxl.Cell;
    import jxl.Sheet;
    import jxl.Workbook;
    import jxl.read.biff.BiffException;
    import jxl.write.Label;
    import jxl.write.WritableCell;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;
    import jxl.write.WriteException;
    import jxl.write.biff.RowsExceededException;
    
    import org.apache.log4j.Logger;
    
    import com.company.department.team.test.Configuration;
    
  • Saikat
    Saikat over 10 years
    Nothing happens with <key>CTRL+SHIFT+o</key>
  • Zoltán
    Zoltán over 10 years
    Just wondering - what happens if you declare the log4j import before the jxl imports? Try to play around with the order of import groups.
  • Saikat
    Saikat over 10 years
    The order there is = Java > Javax > JUnit > Org
  • Saikat
    Saikat over 10 years
    This did not help though as my auto-formatter dragged it down after jxl.
  • Woodchuck
    Woodchuck over 3 years
    Fyi, if you want to skip the import order checking, you can add the CustomImportOrder check to a suppressions filter file (maven.apache.org/plugins/maven-checkstyle-plugin/examples/…‌​).