[ 
https://issues.apache.org/jira/browse/PDFBOX-4671?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17386132#comment-17386132
 ] 

Clifford Dann edited comment on PDFBOX-4671 at 7/23/21, 10:03 AM:
------------------------------------------------------------------

There is a simple answer to this, and if someone knows a person that alters 
this code point them to this answer as this works for JEE containers and any 
other form of way you may load you class loaders.
{code:java}
change org.apache.pdfbox.jbig2.util.ServiceLookup<B>{code}
change method
{code:java}
public Iterator<B> getServices(Class<B> cls, ClassLoader clsLoader) {{code}
To be
{code:java}
public Iterator<B> getServices(Class<B> cls, ClassLoader clsLoader) {   
        
                Iterator<B> services;
                final String name = cls.getName();

                if (clsLoader != null) {
                        services = getServices(name, clsLoader);
                        if (services != null) {
                                return services;
                        }
                }               
                
                services = getServices(name, 
java.lang.Thread.currentThread().getContextClassLoader());
                if (services != null) {
                        return services;
                }

                services = getServices(name, cls.getClassLoader());
                if (services != null) {
                        return services;
                }

                services = getServices(name, 
ClassLoader.getSystemClassLoader());
                if (services != null) {
                        return services;
                }

                throw new java.lang.RuntimeException("Can not find any services 
for " + name);  
}
{code}
and add method

 
{code:java}
private Iterator<B> getServices(String name, ClassLoader clsLoader) {   
        try {
                        Iterator<B> services = (Iterator<B>) 
ServiceLoader.load((Class<B>) clsLoader.loadClass(name), clsLoader).iterator();
                        if (services.hasNext()) {
                                return services;
                        }
                } catch (ClassNotFoundException ex) {
                }

                return null;
}
{code}
It comes down to the problem that java.util.ServiceLoader<S>  has a lot of 
service.isAssignableFrom(clazz) and this requires that both code from the same 
class loader or it will fail, and this is what is happening in JEE containers 
as there are a load of class loaders, and it dose not pick the right one.

when using .class of a object, it dose not always pick the class that is in 
your current class loader path, I do not know why, it is most probably down to 
the original loader, but I assume this is only a problem with JEE containers as 
each war file has its own class loader and the original class could have been 
loaded by say Tomcat.

If you compare .class.getClassLoader() with 
java.lang.Thread.currentThread().getContextClassLoader() they are different 
which is why it fails to find items.

 


was (Author: cmd1234):
There is a simple answer to this, and if someone knows a person that alters 
this code point them to this answer as this works for JEE containers and any 
other form of way you may load you class loaders.
{code:java}
change org.apache.pdfbox.jbig2.util.ServiceLookup<B>{code}
change method
{code:java}
public Iterator<B> getServices(Class<B> cls, ClassLoader clsLoader) {{code}
To be
{code:java}
public Iterator<B> getServices(Class<B> cls, ClassLoader clsLoader) {   
        
                Iterator<B> services;
                final String name = cls.getName();

                if (clsLoader != null) {
                        services = getServices(name, clsLoader);
                        if (services != null) {
                                return services;
                        }
                }               
                
                services = getServices(name, 
java.lang.Thread.currentThread().getContextClassLoader());
                if (services != null) {
                        return services;
                }

                services = getServices(name, cls.getClassLoader());
                if (services != null) {
                        return services;
                }

                services = getServices(name, 
ClassLoader.getSystemClassLoader());
                if (services != null) {
                        return services;
                }

                throw new java.lang.RuntimeException("Can not find any services 
for " + name);  
}
{code}
and add method

 
{code:java}
private Iterator<B> getServices(String name, ClassLoader clsLoader) {   
        try {
                        Iterator<B> services = (Iterator<B>) 
ServiceLoader.load((Class<B>) clsLoader.loadClass(name), clsLoader).iterator();
                        if (services.hasNext()) {
                                return services;
                        }
                } catch (ClassNotFoundException ex) {
                }               return null;
}
{code}
It comes down to the problem that java.util.ServiceLoader<S>  has a lot of 
service.isAssignableFrom(clazz) and this requires that both code from the same 
class loader or it will fail, and this is what is happening in JEE containers 
as there are a load of class loaders, and it dose not pick the right one.

when using .class of a object, it dose not always pick the class that is in 
your current class loader path, I do not know why, it is most probably down to 
the original loader, but I assume this is only a problem with JEE containers as 
each war file has its own class loader and the original class could have been 
loaded by say Tomcat.

If you compare .class.getClassLoader() with 
java.lang.Thread.currentThread().getContextClassLoader() they are different 
which is why it fails to find items.

 

> NoClassDefFoundError: Could not initialize class 
> org.apache.pdfbox.jbig2.JBIG2ImageReader
> -----------------------------------------------------------------------------------------
>
>                 Key: PDFBOX-4671
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-4671
>             Project: PDFBox
>          Issue Type: Bug
>          Components: JBIG2
>    Affects Versions: 3.0.2 JBIG2
>            Reporter: Luis Panadero Guardeño
>            Assignee: Andreas Lehmkühler
>            Priority: Major
>             Fix For: 3.0.4 JBIG2
>
>         Attachments: jbig2 servlet failcase.tar.gz
>
>
> JBIG2 ImageIO plugin sometimes does a NoClassDefFoundError
> I'm using TwelveMonkeys IIOProviderContextListener to fix the problem of 
> ImageIO with servlets, and additionally doing a 
> {{ImageIO}}.{{scanForPlugins}}(). But this keeps happening randomly. Any idea 
> where could be the problem ?
> This is happening with Tomcat 7  running over Oracle Java 8 (latest version)
>  
> {code:java}
> java.lang.NoClassDefFoundError: Could not initialize class 
> org.apache.pdfbox.jbig2.JBIG2ImageReader
>  at 
> org.apache.pdfbox.jbig2.JBIG2ImageReaderSpi.createReaderInstance(JBIG2ImageReaderSpi.java:117)
>  at 
> javax.imageio.spi.ImageReaderSpi.createReaderInstance(ImageReaderSpi.java:320)
>  at javax.imageio.ImageIO$ImageReaderIterator.next(ImageIO.java:529)
>  at javax.imageio.ImageIO$ImageReaderIterator.next(ImageIO.java:513)
>  at org.apache.pdfbox.filter.Filter.findImageReader(Filter.java:155)
>  at org.apache.pdfbox.filter.JBIG2Filter.decode(JBIG2Filter.java:67)
>  at org.apache.pdfbox.cos.COSInputStream.create(COSInputStream.java:84)
>  at org.apache.pdfbox.cos.COSStream.createInputStream(COSStream.java:175)
>  at 
> org.apache.pdfbox.pdmodel.common.PDStream.createInputStream(PDStream.java:241)
>  at 
> org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject.createInputStream(PDImageXObject.java:735)
>  at 
> org.apache.pdfbox.pdmodel.graphics.image.SampledImageReader.from1Bit(SampledImageReader.java:253)
>  at 
> org.apache.pdfbox.pdmodel.graphics.image.SampledImageReader.getRGBImage(SampledImageReader.java:212)
>  at 
> org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject.getImage(PDImageXObject.java:444)
>  at 
> org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject.getImage(PDImageXObject.java:425)
>  at org.apache.pdfbox.rendering.PageDrawer.drawImage(PageDrawer.java:1100)
>  at 
> org.apache.pdfbox.contentstream.operator.graphics.DrawObject.process(DrawObject.java:63)
>  at 
> org.apache.pdfbox.contentstream.PDFStreamEngine.processOperator(PDFStreamEngine.java:869)
>  at 
> org.apache.pdfbox.contentstream.PDFStreamEngine.processStreamOperators(PDFStreamEngine.java:505)
>  at 
> org.apache.pdfbox.contentstream.PDFStreamEngine.processStream(PDFStreamEngine.java:479)
>  at 
> org.apache.pdfbox.contentstream.PDFStreamEngine.processPage(PDFStreamEngine.java:152)
>  at org.apache.pdfbox.rendering.PageDrawer.drawPage(PageDrawer.java:263)
>  at org.apache.pdfbox.rendering.PDFRenderer.renderImage(PDFRenderer.java:321)
>  at org.apache.pdfbox.rendering.PDFRenderer.renderImage(PDFRenderer.java:243)
>  at org.apache.pdfbox.rendering.PDFRenderer.renderImage(PDFRenderer.java:203)
>  at 
> com.digibis.digiimage.pdf.pdfbox.PdfBoxPdfProcessor.getImageFromDocumentPage(PdfBoxPdfProcessor.java:105){code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to