Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.aspose.cells.b.a.b.zh

Am facing NoClassDefFoundError for aspose jar after Java17 upgrade. It was working well in Java 8 in production. I could find the respective package (com.aspose.cells.b.a.b.zh) inside aspose-cells-20.9.8 jar but still this error is thrown during run time API invocation. Please clarify the root cause of the issue.

Providing few lines on how its invoked

HtmlLoadOptions opts = new HtmlLoadOptions();
opts.setAutoFitColsAndRows(true);
opts.setConvertNumericData(false);
opts.setMemorySetting(MemorySetting.MEMORY_PREFERENCE);
Workbook wb = new Workbook(htmlFile.getAbsolutePath(), opts);

stack trace:

Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.aspose.cells.b.a.b.zh
    at com.aspose.cells.b.a.b.zg.<init>(Unknown Source)
    at com.aspose.cells.b.a.b.zg.<init>(Unknown Source)
    at com.aspose.cells.zajw.a(Unknown Source)
    at com.aspose.cells.zajw.<init>(Unknown Source)
    at com.aspose.cells.zakq.<init>(Unknown Source)
    at com.aspose.cells.zakr.a(Unknown Source)
    at com.aspose.cells.zakr.<init>(Unknown Source)
    at com.aspose.cells.zakx.<init>(Unknown Source)
    at com.aspose.cells.zaky.a(Unknown Source)
    at com.aspose.cells.zjv.a(Unknown Source)
    at com.aspose.cells.Workbook.a(Unknown Source)
    at com.aspose.cells.Workbook.<init>(Unknown Source)

@rselvarajan

The NoClassDefFoundError you are encountering after upgrading to Java 17 may be related to compatibility issues between the Aspose.Cells library and the newer Java version. This error typically indicates that the Java Virtual Machine (JVM) is unable to find a class that was present during compile time but is not available at runtime.

Here are a few steps you can take to troubleshoot and potentially resolve the issue:

  1. Check Library Compatibility: Ensure that the version of Aspose.Cells you are using (20.9.8) is compatible with Java 17. It’s possible that this version was not designed to work with Java 17, and you may need to upgrade to a newer version of Aspose.Cells that explicitly supports Java 17.

  2. Update Aspose.Cells: If a newer version is available, consider updating to it. Newer versions often include fixes for compatibility issues with newer Java versions.

  3. Verify Classpath: Make sure that the Aspose.Cells JAR file is correctly included in your project’s classpath. Sometimes, classpath issues can lead to NoClassDefFoundError.

  4. Check for Transitive Dependencies: If your project uses other libraries that depend on Aspose.Cells, ensure that all dependencies are compatible with Java 17. Conflicts between library versions can sometimes cause runtime errors.

  5. Review Stack Trace: The stack trace you provided indicates that the error occurs during the initialization of a class within the Aspose.Cells library. This could be due to missing dependencies or issues within the library itself. Investigating the specific class mentioned in the error may provide further insights.

  6. Consult Documentation: Review the Aspose.Cells documentation for any notes on compatibility with Java versions, especially regarding any breaking changes or migration guides.

If these steps do not resolve the issue, consider reaching out to Aspose support or checking their forums for similar issues reported by other users. They may have additional insights or solutions specific to your situation.

If you need further assistance, feel free to ask!

yes please have a person check on this

@rselvarajan,

I tested your scenario/case using older Aspose.Cells for Java v20.10 (I could not find Aspose.Cells for Java v20.9.8 on maven repos., which might not be an official release and could be an interim fix) with JDK21, and it works fine. Here is my test code, and please find attached the output Excel file for your reference.
e.g.,
Sample code:

Workbook workbook = new Workbook();
// Access the first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);

// Access cell A1 and set its value
Cells cells = worksheet.getCells();
Cell cell = cells.get("A1");
cell.setValue("Hello, Aspose!");
        
// Save the workbook
workbook.save("out1.xlsx");

out1.zip (6.4 KB)

It is recommended to upgrade the JDK version and Aspose.Cells for Java version (if possible). Please let us know if the issue persists after the upgrade.

hi @amjad.sahi
Our organization has asked us to use only Java 17 and we have only aspose-cells 20.9.8 in our org common maven repository. can you please test using this and provide your inputs.

one more stack trace i got
Caused by: java.lang.ExceptionInInitializerError: Exception java.lang.InternalError: java.lang.reflect.InvocationTargetException [in thread “ReportGeneration-LV-Thread-4”]
at java.desktop/sun.font.FontManagerFactory$1.run(FontManagerFactory.java:87)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:318)
at java.desktop/sun.font.FontManagerFactory.getInstance(FontManagerFactory.java:75)
at java.desktop/sun.java2d.SunGraphicsEnvironment.getFontManagerForSGE(SunGraphicsEnvironment.java:137)
at java.desktop/sun.java2d.SunGraphicsEnvironment.getAllFonts(SunGraphicsEnvironment.java:156)
at java.desktop/sun.java2d.HeadlessGraphicsEnvironment.getAllFonts(HeadlessGraphicsEnvironment.java:76)
at com.aspose.cells.b.a.b.d.a.zb.(Unknown Source)
at com.aspose.cells.b.a.b.zh.(Unknown Source)
at com.aspose.cells.b.a.b.zg.(Unknown Source)
at com.aspose.cells.b.a.b.zg.(Unknown Source)
at com.aspose.cells.zajw.a(Unknown Source)
at com.aspose.cells.zajw.(Unknown Source)
at com.aspose.cells.zakq.(Unknown Source)
at com.aspose.cells.zakr.a(Unknown Source)
at com.aspose.cells.zakr.(Unknown Source)
at com.aspose.cells.zakx.(Unknown Source)
at com.aspose.cells.zaky.a(Unknown Source)
at com.aspose.cells.zjv.a(Unknown Source)
at com.aspose.cells.Workbook.a(Unknown Source)
at com.aspose.cells.Workbook.(Unknown Source)

AccessController.doPrivileged(new PrivilegedAction() seems to be implemented differently in java8 and 17

@rselvarajan,

As I told you that I could not find Aspose.Cells for Java v20.9.8 @ maven repos. I think your version might be an interim fix and not an official release. Please note, generally, we do not evaluate issues using older versions of the API. Nor we could include fixes to older versions. All the fixes and enhancements and even providing support are based on latest APIs set. So, we still recommend you upgrade Aspose.Cells to latest version (Aspose.Cells for Java v24.10).

Anyways, the error (java.lang.NoClassDefFoundError: Could not initialize class com.aspose.cells.b.a.b.zh) might be related to compatibility issues between Aspose.Cells 20.9.8 and Java 17. You may try:

  1. Aspose.Cells 20.9.8 was released before Java 17. Java 17 includes changes that impact libraries reliant on internal Java classes, particularly in graphics and security, which Aspose.Cells uses. Later versions of Aspose.Cells (around 21.x or newer) were updated to ensure compatibility with Java 17.
  2. Your second stack trace indicates a potential issue with fonts management in a headless (server) environment. Java 17 handles AccessController.doPrivileged and internal reflection differently, which might be causing this font-related issue. Later Aspose versions addresses this by optimizing font handling in newer Java versions.

Some possible options/solutions that you may try:

  1. If you’re working in a headless environment and want to persist with Aspose.Cells for Java v20.9.8, make sure to set the JVM argument -Djava.awt.headless=true. This can reduce font-related initialization errors in server environments.
  2. If possible, update to a more recent version of Aspose.Cells (preferably 21.x or later) in your organization’s Maven repository.

Hope, this helps a bit.

@amjad.sahi thanks for details. The JVM argument -Djava.awt.headless=true is not helping. My organization is asking for release notes from which version of aspose-cells java17 is supported so that they can sync with the maven repository. can you please share the release notes

@rselvarajan,

We recommend using the most recent version of Aspose.Cells for Java, as newer versions have the latest enhancements and fixes. Additionally, we believe that even version 20.x.x should work well with Java 17. Could you please zip and attach the Aspose.Cells for Java version you are currently using, so we can test it with Java 17 on our end?

aspose-cells-20.9.8 (1).7z (7.0 MB)

@amjad.sahi thanks, i have attached the zip

@rselvarajan,

Thanks for the Aspose.Cells for Java JAR.

I tested with Java 17 and it works fine on my end. I used the same code from my previous post.
e.g.,
Sample code:

import com.aspose.cells.*;

public class TestClass1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		System.out.println(Runtime.getRuntime().version());//Check Java used.
		
		System.out.println(CellsHelper.getVersion());//Check Aspose.Cells for Java version used
		
		Workbook workbook = new Workbook();
				
	    // Access the first worksheet
	    Worksheet worksheet = workbook.getWorksheets().get(0);

	    // Access cell A1 and set its value
	    Cells cells = worksheet.getCells();
	    Cell cell = cells.get("A1");
	    cell.setValue("Hello, Aspose!");
	        
	    System.out.println("testin...");

	    // Save the workbook
	    try {
			workbook.save("d:\\files\\out1.xlsx");
			System.out.println("Excel file saved successfully");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	
	}

}

Please find attached the output file.
out1.zip (6.4 KB)

Please see the screenshot of successful code processing using Java 17 with Aspose.Cells for Java v20.9.8.
sc_shot1.png (101.3 KB)

It seems to be an issue with your environment/configuration or your HTML file (fonts used in the file might cause it). Do you get the error with specific code or with specific file? Please provide us more details, along with a sample (runnable) code or project and the sample file(s) you’re using to reproduce the problem on our end. Also, share the complete environment details so we can check it precisely.

@rselvarajan ,

We analyzed your shared stack trace, please try to run the following code only on your environment:

java.awt.Font[] fonts = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();

hi @peyton.xu
when added the provided line, am getting exception

java.awt.Font[] fonts = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();

Exception trace:

Caused by: java.lang.InternalError: java.lang.reflect.InvocationTargetException
at java.desktop/sun.font.FontManagerFactory$1.run(FontManagerFactory.java:87)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:318)
at java.desktop/sun.font.FontManagerFactory.getInstance(FontManagerFactory.java:75)
at java.desktop/sun.java2d.SunGraphicsEnvironment.getFontManagerForSGE(SunGraphicsEnvironment.java:137)
at java.desktop/sun.java2d.SunGraphicsEnvironment.getAllFonts(SunGraphicsEnvironment.java:156)
at java.desktop/sun.java2d.HeadlessGraphicsEnvironment.getAllFonts(HeadlessGraphicsEnvironment.java:76)
at com.paypal.compliance.rfp.report.reportgen.generator.converter.impl.ReportConverter.convertHtmlToExcel(ReportConverter.java:200)
at com.paypal.compliance.rfp.report.reportgen.generator.converter.impl.ReportConverter.convertReport(ReportConverter.java:79)
at com.paypal.compliance.rfp.report.reportgen.generator.ReportingEngine.generateReport(ReportingEngine.java:127)
at com.paypal.compliance.rfp.report.reportgen.service.impl.SimpleReportGenService.generateReport(SimpleReportGenService.java:79)
at com.paypal.compliance.rfp.report.processor.impl.ReportGenerationTask.lambda$fetchRestOfDataAndGenerateReport$5(ReportGenerationTask.java:524)
at java.base/java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1804)
… 4 common frames omitted
Caused by: java.lang.reflect.InvocationTargetException: null
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at java.desktop/sun.font.FontManagerFactory$1.run(FontManagerFactory.java:85)
… 15 common frames omitted
Caused by: java.lang.RuntimeException: Fontconfig head is null, check your fonts or fonts configuration
at java.desktop/sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1271)
at java.desktop/sun.awt.FontConfiguration.readFontConfigFile(FontConfiguration.java:224)
at java.desktop/sun.awt.FontConfiguration.init(FontConfiguration.java:106)
at java.desktop/sun.awt.X11FontManager.createFontConfiguration(X11FontManager.java:706)
at java.desktop/sun.font.SunFontManager$2.run(SunFontManager.java:358)
at java.desktop/sun.font.SunFontManager$2.run(SunFontManager.java:315)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:318)
at java.desktop/sun.font.SunFontManager.(SunFontManager.java:315)
at java.desktop/sun.awt.FcFontManager.(FcFontManager.java:35)
at java.desktop/sun.awt.X11FontManager.(X11FontManager.java:56)
… 21 common frames omitted

@rselvarajan,

Thanks for your feedback and error trace.

Since you are getting the exception (without involving or using Aspose.Cells API), which suggests that your issue is not related to Aspose.Cells for Java by any means and the issue is due to your environment or configurations.

The exception you are encountering, java.lang.RuntimeException: Fontconfig head is null, suggests an issue with font configuration, particularly in a headless environment where no graphical user interface (GUI) is available. This is common in server environments where the system cannot access the default fonts or a graphical subsystem, such as when running in Docker containers or cloud-based servers.

To resolve this issue, you have a few options:

1). Run in Headless Mode
Since you’re likely running in a headless environment, you should configure Java to run in headless mode. Headless mode allows Java to operate without requiring access to a display or graphics environment, which is essential when running in environments like Azure or Docker.

You can enable headless mode by adding the following system property at the start of your Java application:

java

System.setProperty("java.awt.headless", "true");

Or set it via the command line when launching the JVM:

-Djava.awt.headless=true

This prevents Java from attempting to use any GUI-related classes, like font configuration, which is the root of the issue.

2). Install the Necessary Fonts
If you’re working in an environment where you can install packages, you can install font-related packages to ensure that basic fonts are available. For Linux systems, you could install font packages such as:

sudo apt-get install -y fontconfig

On some systems, it might also be necessary to install specific fonts, such as:

sudo apt-get install -y ttf-dejavu-core

@amjad.sahi thanks for the details. Since font dependency is coming indirectly through aspose-cells jar, is there any way this error will be fixed automatically by upgrading to any aspose-cells jar version? if no other possible way, i will check with my organization on the steps that you have provided with as we dont have sudo permissions to run commands on production environments.

@rselvarajan ,

Upgrading the version of Aspose.Cells for Java will not help solve the issue.
Please take your time and let us know your feedback.

@rselvarajan ,

Is the exception fixed on your environment?

Also, we’re investigate the possiblity to handle the exception inner Aspose.Cells. Could you please share us a Dockerfile which simulates your environment? So we can reproduce the exception with the Dockerfile on our side.

hi @peyton.xu after a long struggle, our organization bundled fontsconfig along with the application docker image and now it works. but the expectation was aspose to bundle fontsconfig package so that we dont need additional steps in our application. since we dont own docker image creation steps, i couldnt provide the required details. But i can share a sample html file with font which when provided to aspose library could regenerate the issue.

@rselvarajan,

We are sorry but Aspose.Cells does not bundle Fontconfig with the package, so it is user’s responsibility to install/have all the necessary fonts in his environment. Maybe, you could have custom fonts directory. If installing Fontconfig isn’t feasible, you can specify a custom directory for fonts. You can copy/bundle the required font files with your application and point Aspose.Cells to this directory using the line of code at the start of your application/program:

FontConfigs.setFontFolder("/path/to/fonts", true);

See the document on configuring fonts for your complete reference.

sure thanks, i will try it out

@rselvarajan
You are welcome. Please take your time to try the suggested solutions. Hopefully, your issue will be sorted out. If you have any questions, please feel free to contact us at any time.