Cannot access class com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl

14.4k Views Asked by At

My program needs to convert an xml file to another xml file compatible with sending invoices through our company program.

The compilation of the code happens without problems, while when I start the .bat file I get this error:

Exception in thread "main" java.lang.IllegalAccessError: 
class controller.ConversionWorker (in unnamed module @0x71c8becc) 
cannot access class com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl (in module java.xml) 
because module java.xml does not export com.sun.org.apache.xerces.internal.jaxp.datatype to unnamed module @0x71c8becc

This is the code where the exception is generated:

 private XMLGregorianCalendar stringToXMLGregorian(String data, String pattern) {
    Calendar date = Calendar.getInstance();
    SimpleDateFormat format = new SimpleDateFormat(pattern);
    try {
        date.setTime(format.parse(data));
    } catch (ParseException e) {
        e.printStackTrace();
        Log.debug("ERRORE DI CONVERSIONE DATA: " + data);
    }

    XMLGregorianCalendar result = new XMLGregorianCalendarImpl();
    result.setYear(date.get(Calendar.YEAR));
    result.setMonth(date.get(Calendar.MONTH) + 1);
    result.setDay(date.get(Calendar.DAY_OF_MONTH));

    if (!pattern.equals(DATE_PATTERN)) {
        result.setHour(date.get(Calendar.HOUR_OF_DAY));
        result.setMinute(date.get(Calendar.MINUTE));
    }

    return result;

}

This code was written by a former colleague of mine and only he worked on it. Honestly I am not so much informed in java and how it is connected to gradle but unfortunately this code needs to be revised due to an error that occurs

In gradle-wrapper.properties i've add the:

distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip

I have installed the latest version of Java, Gradle and i build the code in Intellij

How can i solve this?

1

There are 1 best solutions below

0
On

You are not supposed to use class XMLGregorianCalendarImpl in your own code, it's an internal class in the JDK and therefore it cannot be used directly.

Create an instance of XMLGregorianCalendar using DatatypeFactory instead:

import javax.xml.datatype.DatatypeFactory;

// ...

XMLGregorianCalendar result = DatatypeFactory.newInstance().newXMLGregorianCalendar();

See java.xml.datatype.DatatypeFactory