phax / ph-ubl

Java library for reading and writing UBL 2.0, 2.1, 2.2, 2.3 and 2.4 documents
Apache License 2.0
111 stars 40 forks source link

Implementation of JAXB-API has not been found on module path or classpath. #43

Closed yusufdonmez closed 2 years ago

yusufdonmez commented 2 years ago

I try to use UBL2.1 version. in test method I got the error when try to create invoice from scratch.

public static void main(String[] args) {
        String sCurrency = "EUR";

        // Create domain object
        InvoiceType aInvoice = new InvoiceType ();

        // Fill it
        aInvoice.setID ("Dummy Invoice number");
        aInvoice.setIssueDate (PDTFactory.getCurrentXMLOffsetDateUTC ());

        SupplierPartyType aSupplier = new SupplierPartyType ();
        aInvoice.setAccountingSupplierParty (aSupplier);

        CustomerPartyType aCustomer = new CustomerPartyType ();
        aInvoice.setAccountingCustomerParty (aCustomer);

        MonetaryTotalType aMT = new MonetaryTotalType ();
        aMT.setPayableAmount (BigDecimal.TEN).setCurrencyID (sCurrency);
        aInvoice.setLegalMonetaryTotal (aMT);

        InvoiceLineType aLine = new InvoiceLineType ();
        aLine.setID ("1");

        ItemType aItem = new ItemType ();
        aLine.setItem (aItem);

        aLine.setLineExtensionAmount (BigDecimal.TEN).setCurrencyID (sCurrency);

        aInvoice.addInvoiceLine (aLine);

        // Add some TaxTotal
        /**
         * <pre>
        <cac:TaxSubtotal>
          <cbc:TaxableAmount currencyID="EUR">10.00</cbc:TaxableAmount>
          <cbc:TaxAmount currencyID="EUR">10.00</cbc:TaxAmount>
          <cac:TaxCategory>
            <cbc:ID schemeID="UNCL5305" schemeAgencyID="6">Z</cbc:ID>
            <cbc:Percent>10.00</cbc:Percent>
            <cac:TaxScheme>
              <cbc:ID schemeAgencyID="6" schemeID="UN/ECE 5153">VAT</cbc:ID>
            </cac:TaxScheme>
          </cac:TaxCategory>
        </cac:TaxSubtotal>
         * </pre>
         */
        {
          final TaxSubtotalType aTaxSubtotal = new TaxSubtotalType ();
          aTaxSubtotal.setTaxableAmount (BigDecimal.TEN).setCurrencyID (sCurrency);
          aTaxSubtotal.setTaxAmount (BigDecimal.TEN).setCurrencyID (sCurrency);

          final TaxCategoryType aTaxCategory = new TaxCategoryType ();
          final IDType aTCID = new IDType ();
          aTCID.setSchemeID ("UNCL5305");
          aTCID.setSchemeAgencyID ("6");
          aTCID.setValue ("Z");
          aTaxCategory.setID (aTCID);

          aTaxCategory.setPercent (BigDecimal.TEN);

          final TaxSchemeType aTaxScheme = new TaxSchemeType ();
          final IDType aTSID = new IDType ();
          aTSID.setSchemeID ("UNCL5305");
          aTSID.setSchemeAgencyID ("6");
          aTSID.setValue ("VAT");
          aTaxScheme.setID (aTSID);
          aTaxCategory.setTaxScheme (aTaxScheme);

          aTaxSubtotal.setTaxCategory (aTaxCategory);

          final TaxTotalType aTaxTotal = new TaxTotalType ();
          aTaxTotal.setTaxAmount (BigDecimal.TEN).setCurrencyID (sCurrency);
          aTaxTotal.addTaxSubtotal (aTaxSubtotal);
          aInvoice.addTaxTotal (aTaxTotal);
        }

        // Write to disk
        UBL21Writer.invoice().write(aInvoice, new File ("target/dummy-invoice.xml"));
    }

pom.xml

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.helger.ubl</groupId>
      <artifactId>ph-ubl-parent-pom</artifactId>
      <version>6.6.3</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

  <dependencies>

    <!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl -->
    <dependency>
      <groupId>com.sun.xml.bind</groupId>
      <artifactId>jaxb-impl</artifactId>
      <version>3.0.0</version>
    </dependency>

    <dependency>
      <groupId>com.helger.ubl</groupId>
      <artifactId>ph-ubl21-codelists</artifactId>
      <version>6.6.3</version>
    </dependency>
    <dependency>
      <groupId>com.helger.ubl</groupId>
      <artifactId>ph-ubl21</artifactId>
      <version>6.6.3</version>
    </dependency>

  </dependencies>

Error:

Exception in thread "main" java.lang.IllegalArgumentException: Failed to create JAXB context for package 'oasis.names.specification.ubl.schema.xsd.invoice_21' using ClassLoader jdk.internal.loader.ClassLoaders$AppClassLoader@4459eb14
        at com.helger.jaxb.JAXBContextCacheKey._createFromPackageAndClassLoader(JAXBContextCacheKey.java:165)
        at com.helger.jaxb.JAXBContextCacheKey.createJAXBContext(JAXBContextCacheKey.java:202)
        at com.helger.jaxb.JAXBContextCache.lambda$new$0(JAXBContextCache.java:81)
        at com.helger.commons.cache.MappedCache.getFromCache(MappedCache.java:337)
        at com.helger.jaxb.JAXBContextCache.getFromCache(JAXBContextCache.java:124)
        at com.helger.jaxb.JAXBContextCache.getFromCache(JAXBContextCache.java:166)
        at com.helger.jaxb.builder.AbstractJAXBBuilder.getJAXBContext(AbstractJAXBBuilder.java:164)
        at com.helger.jaxb.builder.AbstractWritingJAXBBuilder.createMarshaller(AbstractWritingJAXBBuilder.java:80)
        at com.helger.jaxb.builder.JAXBWriterBuilder.createMarshaller(JAXBWriterBuilder.java:232)
        at com.helger.jaxb.builder.JAXBWriterBuilder.write(JAXBWriterBuilder.java:298)
        at com.helger.jaxb.IJAXBWriter.write(IJAXBWriter.java:381)
        at com.helger.jaxb.IJAXBWriter.write(IJAXBWriter.java:234)
        at com.helger.jaxb.IJAXBWriter.write(IJAXBWriter.java:195)
        at Test.main(Test.java:98)
Caused by: javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
 - with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory]
        at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:131)
        at javax.xml.bind.ContextFinder.find(ContextFinder.java:318)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:478)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:435)
        at com.helger.jaxb.JAXBContextCacheKey._createFromPackageAndClassLoader(JAXBContextCacheKey.java:155)
        ... 13 more
Caused by: java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
        at javax.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:92)
        at javax.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:125)
        at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:128)
        ... 17 more
phax commented 2 years ago

Please use JAXB 2.x only. JAXB 3 requires Java 11 as the basis, but my code uses Java 1.8 for everything. hth

yusufdonmez commented 2 years ago

Thanks @phax This solves my issue:

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.3.3</version>
</dependency>