Forum Discussion

krogold's avatar
Regular Contributor
5 years ago

how to use java-diff-utils


for a test, I need to compare files in a groovy script and I found out that a library could help me perform this in a simple way. However, though there are quite a lot of examples that I can rely on in order to build my own methods, I am unable to use this lib. I cannot find anywhere how to import / use this lib in my groovy script.

I set the java-diff-utils-2.2.0.jar and jsr305-3.0.0.jar in bin/ext and I use the following example in a groovy step, just for trying, but it fails (unable to resolve class Patch @ line 70, column 19)


I guess that import is wrong, but I'm not fluent in groovy/java and not familiar at all with dependency and lib handling

Could anyone help me figure out what's wrong ?


import difflib.*;

import java.util.AbstractList;
import java.util.LinkedList;
import java.util.List;

public class TestDiffUtils {

    public TestDiffUtils() {


    // Helper method to read the files to compare into memory, convert them to a list of Strings which can be used by the DiffUtils library for comparison
    private static List fileToLines(String filename) {
        List lines = new LinkedList();
        String line;
        try {
            URL path = TestDiffUtils.class.getResource(filename);
            File f = new File(path.getFile());
            BufferedReader input = new BufferedReader(new FileReader(f));
            while ((line = input.readLine()) != null) {
        } catch (IOException e) {

        return lines;

    // Helper method to convert a String to List<Character> : to be used with DiffUtils.diff() method when finding character diffs within the line diffs
    public static List<Character> asList(final String string) {
        return new AbstractList<Character>() {
            public int size() { return string.length(); }
            public Character get(int index) { return string.charAt(index); }

    private static void performDiff(String testName) {
        String origFileName = null;
        String revisedFileName = null;

        Boolean continueTest = true;    // continue unless we can't

        if( testName.compareTo("large-file-test") == 0 )
            origFileName = "test_large_file.xml";
            revisedFileName = "test_large_file_revised.xml";
        }else if( testName.compareTo("small-file-test") == 0 ){
            origFileName = "originalFile.txt";
            revisedFileName = "revisedFile.txt";
        }else if( testName.compareTo("large-file-test-single-line") == 0 ){
            origFileName = "test_large_file_SingleLine.xml";
            revisedFileName = "test_large_file_revised_SingleLine.xml";
            continueTest = false;

        if( continueTest && origFileName != null && revisedFileName != null ) {
            // Convert the orig and revised files to List<String> format that DiffUtils.diff() uses
            List<String> originalLines = fileToLines(origFileName);
            List<String> revisedLines = fileToLines(revisedFileName);

            // Get the line-by-line diffs
            Patch patch = DiffUtils.diff(originalLines, revisedLines);
            List<Delta> deltas = patch.getDeltas();
            for (Delta delta : deltas) {
                // The line in the orig file that this current diff occurs at
                int lineOfDiffInOrig = delta.getOriginal().getPosition() + 1;

                // Only continue with the diff if the revised lines is not empty (we will get IndexOu0tOfBoundsExceptions if we don't do this check)
                if (delta.getOriginal().getLines().size() > 0 && delta.getRevised().getLines().size() > 0) {
                    // Get orig and revised lines in List<Character> format
                    List<Character> origChars = asList(((String) delta.getOriginal().getLines().get(0)));
                    List<Character> revisedChars = asList(((String) delta.getRevised().getLines().get(0)));

                    // Get the character-by-character diffs
                    Patch deltaPatch = DiffUtils.diff(origChars, revisedChars);
                    List<Delta> strDeltas = deltaPatch.getDeltas();
                    for (Delta strDelta : strDeltas) {
                        int charPosOfDiffInOrig = strDelta.getOriginal().getPosition() + 1;
                        int lengthOfDiffInOrig = charPosOfDiffInOrig + strDelta.getOriginal().size();

                        System.out.println("Line" + lineOfDiffInOrig + " : [" + charPosOfDiffInOrig + "," + lengthOfDiffInOrig + "]");

    public static void main(String[] args) {

thanks for any help



  • Hi krogold, thank’s for creating this topic.  

    This article contains an example of using third-party libraries in ReadyAPI, perhaps, you will find this helpful.


3 Replies

  • sonya_m's avatar
    SmartBear Alumni (Retired)

    Hi krogold, thank’s for creating this topic.  

    This article contains an example of using third-party libraries in ReadyAPI, perhaps, you will find this helpful.


    • krogold's avatar
      Regular Contributor

      Well, thanks for replying but I already tried for another lib and it worked, my problem is really to know how to import this specific library.

      I followed an example but it ended up wrong :(

      I'll go through the process again, following the examples, I may find how to solve my problem

      • sonya_m's avatar
        SmartBear Alumni (Retired)

        I see. Good luck with your investigation, do let us know how this goes!