Page 1 of 1

Executable Jar Problem (Docx4j Batik)

PostPosted: Fri Jan 04, 2013 2:09 am
by angelpolo
Hello,
I have been working in a java project with Eclipse (Juno version), with docx4j.

Within the Eclipse environment the project runs perfectly, but when i create an executable jar form it trhows an JacaClass NotFound Exeption with Batik. ("java.lang.NoClassDefFoundError: org/apache/batik/bridge/UserAgent").

I tried to add the Batik FOP jar to the prject buildpath, but i have a similar problem, Runs perfectly on Eclipse but it throws an Exeption ("org.docx4j.openpackaging.exceptions.Docx4JException: Error checking image format") from the executable jar.

I tried changing the java version from 1.6 to 1.7 but the problem persist.

I dont know what i need to do to create a functional Executable jar.
Thanks,

Sorry about my english, is not my first language.

Re: Executable Jar Problem (Docx4j Batik)

PostPosted: Mon Jan 07, 2013 3:44 pm
by jason
angelpolo wrote:("java.lang.NoClassDefFoundError: org/apache/batik/bridge/UserAgent"


That NoClassDefFoundError should not be a problem. See docx-java-f6/batik-dependency-log4j-dependency-t1045.html

I suspect you have a different issue, or no issue. Does it produce a PDF?

The pom.xml of current source now fetches that class, since it contains:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
        <dependency>
                <groupId>org.apache.xmlgraphics</groupId>
                <artifactId>fop</artifactId>
                <version>1.0</version>
                <exclusions>
                        <exclusion>
                                <artifactId>avalon-framework-api</artifactId>
                                <groupId>org.apache.avalon.framework</groupId>
                        </exclusion>
                        <exclusion>
                                <artifactId>avalon-framework-impl</artifactId>
                                <groupId>org.apache.avalon.framework</groupId>
                        </exclusion>
                </exclusions>
        </dependency>
        <!-- these two are to correct issues in fop dependency -->
        <dependency>
                <groupId>org.apache.avalon.framework</groupId>
                <artifactId>avalon-framework-api</artifactId>
                <version>4.3.1</version>
        </dependency>
        <dependency>
                <groupId>org.apache.avalon.framework</groupId>
                <artifactId>avalon-framework-impl</artifactId>
                <version>4.3.1</version>
        </dependency>
 
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


This results in the dependency tree:

Code: Select all
[INFO] +- org.apache.xmlgraphics:fop:jar:1.0:compile
[INFO] |  +- org.apache.xmlgraphics:batik-svg-dom:jar:1.7:compile
[INFO] |  |  +- org.apache.xmlgraphics:batik-anim:jar:1.7:compile
[INFO] |  |  +- org.apache.xmlgraphics:batik-css:jar:1.7:compile
[INFO] |  |  +- org.apache.xmlgraphics:batik-dom:jar:1.7:compile
[INFO] |  |  +- org.apache.xmlgraphics:batik-parser:jar:1.7:compile
[INFO] |  |  +- org.apache.xmlgraphics:batik-util:jar:1.7:compile
[INFO] |  |  +- xml-apis:xml-apis:jar:1.3.04:compile
[INFO] |  |  \- xml-apis:xml-apis-ext:jar:1.3.04:compile
[INFO] |  +- org.apache.xmlgraphics:batik-bridge:jar:1.7:compile
[INFO] |  |  +- org.apache.xmlgraphics:batik-script:jar:1.7:compile
[INFO] |  |  \- org.apache.xmlgraphics:batik-xml:jar:1.7:compile
[INFO] |  +- org.apache.xmlgraphics:batik-awt-util:jar:1.7:compile
[INFO] |  +- org.apache.xmlgraphics:batik-gvt:jar:1.7:compile
[INFO] |  +- org.apache.xmlgraphics:batik-transcoder:jar:1.7:compile
[INFO] |  |  \- org.apache.xmlgraphics:batik-svggen:jar:1.7:compile
[INFO] |  +- org.apache.xmlgraphics:batik-extension:jar:1.7:compile
[INFO] |  \- org.apache.xmlgraphics:batik-ext:jar:1.7:compile

Re: Executable Jar Problem (Docx4j Batik)

PostPosted: Fri Jan 11, 2013 1:48 am
by angelpolo
Jason thanks for your reply,
The Batik problem was solved, in one topic of the forum you sugested to change the docxj4 properties of the log4j configurator.

"docx4j.Log4j.Configurator.disabled=true"

With this change the Batik problem doesn't persist, but the program still throws an "Error Checking Image format" exception when its executed from the jar, because when i run the program on eclipse runs perfectly.

org.docx4j.openpackaging.exceptions.Docx4JException: Error checking image format
at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.ensureFormatIsSupported(BinaryPartAbstractImage.java:429)
at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.ensureFormatIsSupported(BinaryPartAbstractImage.java:331)
at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.createImagePart(BinaryPartAbstractImage.java:225)
at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.createImagePart(BinaryPartAbstractImage.java:144)
at com.replacer.services.msword.MSWordFunctions.newImage(MSWordFunctions.java:128)
at com.replacer.services.msword.MSWordFunctions.resizeAndInsertDocImage(MSWordFunctions.java:108)
at com.replacer.services.msword.MSWordExportService.writeSubProcessElements(MSWordExportService.java:64)
at com.replacer.services.msword.MSWordExportService.buildDocumentation(MSWordExportService.java:42)
at com.replacer.services.XMLReplacerService.buildXML(XMLReplacerService.java:39)
at com.replacer.windows.HTMLReplacerWindow.<init>(HTMLReplacerWindow.java:24)
at com.replacer.windows.HTMLReplacerWindow.main(HTMLReplacerWindow.java:33)
Caused by: java.io.IOException: Cannot run program "imconvert": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.convertToPNG(BinaryPartAbstractImage.java:905)
at org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage.ensureFormatIsSupported(BinaryPartAbstractImage.java:413)
... 10 more
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find th
e file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 16 more

Answering your cuestion, the program doesnt produce a PDF, just generates a Word File.

Thanks a lot.
Angel.

Re: Executable Jar Problem (Docx4j Batik)

PostPosted: Fri Jan 11, 2013 8:14 am
by jason
angelpolo wrote:Cannot run program "imconvert": CreateProcess error=2, The system cannot find the file specified


As per the error message, the system is trying to use imconvert to convert an image to PNG format. This is done for certain odd image types. (Could you have a look at your docx, and see what image types it contains?)

It uses the command:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
Process p = Runtime.getRuntime().exec("imconvert -density " + density + " -units PixelsPerInch - png:-");  
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


(the convert program renamed to imconvert)

Failure to find that though should degrade more gracefully....

Re: Executable Jar Problem (Docx4j Batik)

PostPosted: Sat Feb 02, 2013 1:48 am
by angelpolo
Jason, Thanks a lot for your reply.
Im sorry for the delay in the answer, i went on holidays.

The program is inserting "jpg" images files into the .docx with this method:

public void resizeAndInsertDocImage(String path) throws Exception {
File file = new File(FilePath.getPath() +"//" +path);
java.io.InputStream is = new java.io.FileInputStream(file);
long length = file.length();
if (length > Integer.MAX_VALUE) {
System.out.println("File too large!!");
}
byte[] bytes = new byte[(int)length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
if (offset < bytes.length) {
System.out.println("Could not completely read file "+file.getName());
}
is.close();
String filenameHint = null;
String altText = null;
int id1 = 0;
int id2 = 1;
P p = newImage( wpmlp, bytes, filenameHint, altText,id1, id2 );
documentPart.addObject(p);
}


Can the problem be the name of the file? because im taking the files from a html folder resources they are named like: _AIhQQPkyEeGwy6d8RSiN5g_SP000002__ANJ4r_kyEeGwy6d8RSiN5g.jpg

There is a reason why this problem does not occur running the program with eclipse?

Re: Executable Jar Problem (Docx4j Batik)

PostPosted: Sat Feb 02, 2013 7:09 am
by jason
The file name shouldn't be a problem.

I'd ask you to post or email me the jpg image which is causing the problem, but the fact that it works for you in Eclipse means this is probably a waste of time.

More likely, check that you are running the same docx4j jar and xmlgraphics-commons jar

Re: Executable Jar Problem (Docx4j Batik)

PostPosted: Thu Feb 07, 2013 7:42 am
by Mr_Mig
I have ran in the similar issue lately.
I was building the executable jar with maven assembly plugin and the thorough debugging showed me, that assembly:sinlge goal owerwrites the META-INF/services definitions (actually FOP library overwrites the xmlgraphics services).
The problem was that there was not enough ImagePreloader classes. So I changed the build process to the maven shade plugin with
Code: Select all
   <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>

which merges the META-INF/service resources ;)

So, I suggest you to unzip your resulting jar and look into the
Code: Select all
META-INF/services/org.apache.xmlgraphics.image.loader.spi.ImagePreloader

file. It must contain 9 entries:
Code: Select all
org.apache.xmlgraphics.image.loader.impl.PreloaderTIFF
org.apache.xmlgraphics.image.loader.impl.PreloaderGIF
org.apache.xmlgraphics.image.loader.impl.PreloaderJPEG
org.apache.xmlgraphics.image.loader.impl.PreloaderBMP
org.apache.xmlgraphics.image.loader.impl.PreloaderEMF
org.apache.xmlgraphics.image.loader.impl.PreloaderEPS
org.apache.xmlgraphics.image.loader.impl.imageio.PreloaderImageIO
org.apache.fop.image.loader.batik.PreloaderWMF
org.apache.fop.image.loader.batik.PreloaderSVG