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();