Page 1 of 1

Odd problem when compiling with Java 7

PostPosted: Fri Jul 24, 2015 3:58 am
by arimmer
We have used xlsx4j to produce a number of reports which all work fine when we compile our code using Java 6 but not when compiling with Java 7!

We use a number of unusual characters in our reports, and we use code like the following to add them where required (similar code is used for other characters):
Code: Select all
public static CTRElt getDoubleAngleWithSpaces()
  {
    if (doubleAngleWithSpaces == null)
    {
      doubleAngleWithSpaces = new CTRElt();
      Character dbleAngle = '\u00BB';
      CTXstringWhitespace strDoubleAngleNSpace = Context.getsmlObjectFactory().createCTXstringWhitespace();
      strDoubleAngleNSpace.setValue( " " + dbleAngle.toString() + " " );
      strDoubleAngleNSpace.setSpace("preserve");
      doubleAngleWithSpaces.setT(strDoubleAngleNSpace );
      /* Add some property info as MS Excel gets awkward if there is no property info preceeding something with properties */
      CTRPrElt propsDoubleAngle = Context.getsmlObjectFactory().createCTRPrElt();

      CTColor colorGrey = Context.getsmlObjectFactory().createCTColor();
      colorGrey.setTheme( 1L );
      colorGrey.setTint( NOUGHT_POINT_SIX );
      JAXBElement<CTColor> jbGreyColor = Context.getsmlObjectFactory().createCTFontColor( colorGrey );
      propsDoubleAngle.getRFontOrCharsetOrFamily().add( jbGreyColor );

      doubleAngleWithSpaces.setRPr( propsDoubleAngle );
    }

    return doubleAngleWithSpaces;
  }

When compiled with Java 6 the above code gives a double right pointing arrow (double greater than), but with Java 7 it gives a black diamond with a white question mark inside (may just be what Excel displays unknown characters as?).

Also many of our reports have grouping of rows, and should initially open with the grouped rows fully collapsed. Again this works when compiled with Java 6 but not when compiled with Java 7. The grouping is done using the following code:
Code: Select all
public void setOutlineLevelForRow( long rowId, int outlineLevel, boolean isCollapsed, boolean isHidden )
  {
    XLSXRow xLSXRow = gridRows.get( rowId );
    Row row = xLSXRow.getRow();
    //noinspection NumericCastThatLosesPrecision
    row.setOutlineLevel( (short) outlineLevel );
    row.setCollapsed( isCollapsed );
    row.setHidden( isHidden );
    if (outlineLevel > maxRowOutlineLevel)
    {
      setRowOutlineLevelOnSheet( outlineLevel );
      maxRowOutlineLevel = outlineLevel;
    }
  }

  private void setRowOutlineLevelOnSheet( int outlineLevel )
  {
    if (theWorkSheet.getJaxbElement().getSheetFormatPr() == null)
    {
      CTSheetFormatPr sheetFormatPr = new CTSheetFormatPr();
      theWorkSheet.getJaxbElement().setSheetFormatPr( sheetFormatPr );
    }
    //noinspection NumericCastThatLosesPrecision
    theWorkSheet.getJaxbElement().getSheetFormatPr().setOutlineLevelRow( (short) outlineLevel );
    //noinspection MagicNumber
    theWorkSheet.getJaxbElement().getSheetFormatPr().setDefaultRowHeight(
            15.0D );   // required for automatic collapsing
  }


I assume others are using Java 7 without problems so can anyone give me any pointers as to where the problems might be occurring?

On further investigation I notice an error as xlsx4j starts up which I suspect is the reason for these problems:
Code: Select all
Context                             - java.vendor=Oracle Corporation
23-Jul-2015 18:26:52,635 INFO  - Context                             - java.version=1.7.0_21
23-Jul-2015 18:26:52,965 INFO  - NamespacePrefixMapperUtils          - Using NamespacePrefixMapperSunInternal, which is suitable for Java 6
23-Jul-2015 18:26:52,968 DEBUG - ResourceUtils                       - Not using MOXy, since no resource: org/docx4j/wml/jaxb.properties
23-Jul-2015 18:26:52,968 DEBUG - Context                             - org/docx4j/wml/jaxb.properties not found via classloader.
23-Jul-2015 18:26:52,969 DEBUG - ResourceUtils                       - Not using MOXy, since no resource: org/docx4j/wml/jaxb.properties
23-Jul-2015 18:26:52,969 DEBUG - Context                             - org/docx4j/wml/jaxb.properties not found via classloader.
23-Jul-2015 18:26:52,969 INFO  - Context                             - Using Java 6/7 JAXB implementation
23-Jul-2015 18:27:07,284 INFO  - Context                             - Not using MOXy.
23-Jul-2015 18:27:08,195 ERROR - Context                             - Cannot initialize context
javax.xml.bind.JAXBException: Provider com.sun.xml.internal.bind.v2.ContextFactory could not be instantiated: javax.xml.bind.JAXBException: "org.plutext.jaxb.xmldsig" doesnt contain ObjectFactory.class or jaxb.index
- with linked exception:
[javax.xml.bind.JAXBException: "org.plutext.jaxb.xmldsig" doesnt contain ObjectFactory.class or jaxb.index]
   at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:146)
   at javax.xml.bind.ContextFinder.find(ContextFinder.java:347)
   at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:431)
   at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:394)
   at org.docx4j.jaxb.Context.<clinit>(Context.java:151)
   at org.docx4j.openpackaging.parts.JaxbXmlPart.<init>(JaxbXmlPart.java:85)
   at org.docx4j.openpackaging.parts.JaxbXmlPartXPathAware.<init>(JaxbXmlPartXPathAware.java:56)
   at org.docx4j.openpackaging.parts.SpreadsheetML.JaxbSmlPart.<init>(JaxbSmlPart.java:34)
   at org.docx4j.openpackaging.parts.SpreadsheetML.SharedStrings.<init>(SharedStrings.java:16)
   at uk.co.stasys.ier.reports.xLSX.XLSXSpreadSheetIER.<init>(XLSXSpreadSheetIER.java:37)
   at uk.co.stasys.ier.reports.IERXLSXFormatHandler.convertXML(IERXLSXFormatHandler.java:54)
   at uk.co.stasys.reporting.creation.output.ReportOutputGenerator.generateReport(ReportOutputGenerator.java:85)
   at uk.co.stasys.ier.reports.IERReportGenerator.generateReport(IERReportGenerator.java:59)
   at uk.co.stasys.reporting.creation.main.ESmartReport.transformReport(ESmartReport.java:303)
   at uk.co.stasys.reporting.creation.main.ESmartReport.transformReport(ESmartReport.java:278)
   at uk.co.stasys.reporting.creation.main.ESmartReport.createReportParts(ESmartReport.java:199)
   at uk.co.stasys.reporting.creation.main.ESmartReport.generateReport(ESmartReport.java:91)
   at uk.co.stasys.reporting.creation.main.ReportCreationLauncher$LaunchReport.run(ReportCreationLauncher.java:272)
   at java.lang.Thread.run(Thread.java:722)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
   at java.lang.Thread.run(Thread.java:722)
Caused by: javax.xml.bind.JAXBException: "org.plutext.jaxb.xmldsig" doesnt contain ObjectFactory.class or jaxb.index
   at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:216)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:601)
   at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:172)
   at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:132)


Is this likely to be the cause? How do I solve this problem?

Note: I am using Excel 2010 to open both sets of reports, and have not made any changes to the report code, just changed the compiler used.
Java 6 == JDK1.6.0_18
Java 7 == JDK1.7.0_21

Re: Odd problem when compiling with Java 7

PostPosted: Fri Jul 24, 2015 6:34 pm
by jason
Regarding the error, you need to add http://www.docx4java.org/docx4j/docx4j- ... -1.0.0.jar to your classpath.

Regarding the main subjects of your post, how are you compiling?

In Eclipse, you can right click on a .java file to check its encoding.

Maven and Ant both have encoding settings in pom.xml / build.xml

But since it sounds more like the encoding is being screwed up by code, the easiest thing to try first would be to use a known version of the JAXB reference implementation (instead of what ships in Java) via the endorsed dir mechanism, or MOXy.

If you then get identical behaviour with Java 6 and 7 (either both broken, or both working), you'll know its to do with JAXB.

You can also unzip your xlsx and open the relevant part in SC Unipad. If you set it to tell you about errors, it'll confirm whether there is an encoding issue.

Re: Odd problem when compiling with Java 7

PostPosted: Fri Jul 24, 2015 9:00 pm
by arimmer
Adding the jar to my classpath has successfully removed the error message.

The other errors are still occurring - so I'm now looking into the JAXB stuff.

As we use IntelliJIDEA, I don't think Moxy is available. And we don't use Maven so I can't go down that route.

Re: Odd problem when compiling with Java 7

PostPosted: Fri Jul 24, 2015 10:02 pm
by arimmer
Further investigation showed that my other problems were to do with a hard-coded value in my code that used to be the 'double greater than' sign but had changed to the 'black diamond with question mark' sign.

Once I fixed this by using the actual utf-8 code ( ยป ) it all worked fine with the Java 7 JAXB implementation.

Thanks for your help.