Skip to content

Temporary font files are not deleted on Windows #505

@Phillipus

Description

@Phillipus

(With reference to #337)

Jasper Reports 6.21.4
Windows 11 Pro, 24H2, 26100.2605
OpenJDK Temurin-17.0.13+11 and 21.0.5+11

Since JR version 6.13.0 temporary font files are created in the user's temp directory but are not deleted on exit. This is not a problem on Mac or Linux, only Windows.

I've provided a standalone test case to show this:

JasperTest.zip

The code is this:

import java.io.File;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperReport;

public class JasperTest {
    
    static class ExampleDataSource implements JRDataSource {

        @Override
        public boolean next() throws JRException {
            return false;
        }

        @Override
        public Object getFieldValue(JRField jrField) throws JRException {
            return "Test";
        }
    }

    public static void main(String[] args) {
        try {
            File reportFile = new File("report.jrxml");
            JasperReport jrReport = JasperCompileManager.compileReport(reportFile.getAbsolutePath());
            JasperFillManager.fillReport(jrReport, null, new ExampleDataSource());
        }
        catch(JRException ex) {
            ex.printStackTrace();
        }
    }
}
  1. Unzip the attached zip project on Windows
  2. From the Windows command line run this:
    java -cp jasperreports-6.21.4.jar;commons-digester-2.1.jar;commons-logging-1.3.4.jar;commons-collections4-4.4.jar;commons-beanutils-1.10.0.jar JasperTest.java
  3. Note that in the temporary directory (C:\Users\username\AppData\Local\Temp) that there are two font files of the form jr-font123456789123456789.ttf
  4. Run the example again and note two more font files are created

In our real world use of JR more than two temporary font files are created at a time and remain in the temp folder (around 20) and after a few invocations of JR code there could be thousands.

It seems that font files that are created in AwtFontManager are marked to be deleted on exit:

https://github.com/TIBCOSoftware/jasperreports/blob/4df899783f6198a3e7479948356a8a7b75294603/core/src/main/java/net/sf/jasperreports/engine/fonts/AwtFontManager.java#L79-L80

However, when Java calls its method to delete files on exit (in java.io.DeleteOnExitHook), these files are locked and cannot be deleted (File#delete returns false).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions