How to get rid of Checkstyle message 'File does not end with a newline.'

46,306

Solution 1

Put a newline at the end of the file
or
configure CheckStyle not to care.

<module name="Checker">
    <!-- stuff deleted -->
    <module name="NewlineAtEndOfFile">
        <property name="severity" value="ignore" />
    </module>

You also have to tell the Maven Checkstyle plugin to use your checkstyle config file.

<plugin>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <configuration>
        <configLocation>${basedir}/yourCheckstyle.xml</configLocation>
    </configuration>
</plugin>

Solution 2

In my case that was a problem with improper configuration of that checker. By default it uses system default convention of line endings. I was working on windows and the project was using unix-style line endings. I don't think ignoring the warning is the best strategy, so what I've done is:

<module name="Checker">
    <module name="NewlineAtEndOfFile">
        <property name="lineSeparator" value="lf" />
    </module>
</module>

Solution 3

I run into this problem when files are saved as UNIX format instead of DOS. (or other way around if you do your work in Unix)

To fix it, I use a program do convert from UNIX format to DOS and save the file in that format.

In eclipse: File -> Convert Line Delimiters To -> Windows

Solution 4

There's a simpler way to do this. You can specify a suppressions file without overriding the sun checkstyle config file:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <configuration>
        <suppressionsLocation>suppressions.xml</suppressionsLocation>
        <configLocation>config/sun_checks.xml</configLocation>
    </configuration>
</plugin>

Where your suppressions.xml is:

<?xml version="1.0"?>
<!DOCTYPE suppressions PUBLIC
    "-//Puppy Crawl//DTD Suppressions 1.1//EN"
    "http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">

<suppressions>
    <suppress checks="NewlineAtEndOfFile" files=".java" />
</suppressions>
Share:
46,306
Michael Pralow
Author by

Michael Pralow

Updated on June 22, 2020

Comments

  • Michael Pralow
    Michael Pralow almost 4 years

    I'm working with a Maven (jar) Project in Netbeans (Windows), which creates Checkstyle reports with the Maven Checkstyle plugin.

    No matter what I do, I always get the message: File does not end with a newline for Java class files.

    What can I do/configure in either Netbeans or Checkstyle to get rid of the message ?

    Versions of used software:

    • WinXP SP3
    • Netbeans 6.7 RC1 (happens with 6.5 too)
    • Maven 2.0.9
    • Maven Checkstyle Plugin 2.2
    • Java 1.6 (Update 14)