37

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)

9 Answers 9

67

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, for me this is the solution. It works even if other people in the team have their files on Windows checked out with CR-LF line separators
16

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>

2 Comments

"Put a newline at the end of the file." that seems to be impossible with netbeans on windows
It is possible. If you can move the cursor past the last line, then you've got it. If that still does not clear the warning, check Alexey Voinov's answer and make sure that Checkstyle runs again after the config change.
9

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

Comments

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>

1 Comment

This is great if you are forbidden to change the config file, but allowed to change the suppressions file (it happens!). If you may change the config file, it is better to simply remove the unwanted checks from the config file. Less clutter and better performance.
1

Just put A LINE AT END OF THE FILE. It will solve.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

Eclipse ..Window >> Preferences >> Checkstyle >> Select the configuration file >>

  1. Click on "Properties" button >> Uncheck the Protect Checkstyle Configuration file.
  2. Click on "Configure" button >> search for "New Line At End Of File" >> Uncheck the checkbox which you see next to New Line At End Of File.

Comments

0

Add a new line(press enter) at the end of the class where the issue is observed

1 Comment

This has already been mentioned in at least another answer.
0

The most effective way would be to change the line separator on your side. The problem looks like you have a CRLF line separator, but checkstyle expects macOS Unix style\n For example, for Intellij Idea you can use this guide

Comments

-3

//Here is the brute force solution:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;

/**
 *
 * @author hMueller
 */

public class UnixToWindowsEncoding {

//rewrites all .java files in Windows encoding
//a[0] = parent directory
//loops through all subdirectories and fixes whatever .java file it finds
public static void main(String[] a){
    String startdir = a[0];
    fixDir(startdir);

}

private static void fixDir(String dirname){
    try{
        File f = new File(dirname);
        File[] list = f.listFiles();
        for(int i = 0; i < list.length; i++){
            if(list[i].isDirectory()){
                fixDir(list[i].getAbsolutePath());
            }else{
                if(list[i].getName().endsWith(".java")){
                    ArrayList<String> l = new ArrayList<String>();
                    FileReader fr = new FileReader(list[i]);
                    BufferedReader br = new BufferedReader(fr);
                    String line = "";
                    while( (line = br.readLine()) != null){
                        l.add(line);
                    }
                    br.close();
                    fr.close();
                    list[i].delete();
                    FileWriter fwr = new FileWriter(list[i]);
                    for(String s : l){
                        fwr.write(s + "\r\n");
                    }
                    fwr.flush();
                    fwr.close();
                }
            }
        }
    }catch(Exception ioe){
        ioe.printStackTrace();
    }
}

}

1 Comment

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.