Embed Same jar in two different bundles | Community
Skip to main content
vishnu_jangid
October 1, 2022
Question

Embed Same jar in two different bundles

  • October 1, 2022
  • 3 replies
  • 257 views

Hello community,

 

I want to add a dependency specifically webp-imageio-sejda in my current project (project A), for this jar to be available to my bundle I have to embed it. In my AEM instance there already exist a bundle (project B) which  already contains the same dependency and it is also embedded into that bundle. 

Project A pom.xml :

 

..... <Embed-dependency> webp-iamgeio </Embed-dependency> ...... <dependencies> <!-- https://mvnrepository.com/artifact/org.sejda.imageio/webp-imageio --> <dependency> <groupId>org.sejda.imageio</groupId> <artifactId>webp-imageio</artifactId> <version>0.1.6</version> <scope> provided </scope> </dependency> </dependencies>

 

 

 

Project B pom.xml

 

..... <Embed-dependency> webp-iamgeio </Embed-dependency> ...... <dependencies> <!-- https://mvnrepository.com/artifact/org.sejda.imageio/webp-imageio --> <dependency> <groupId>org.sejda.imageio</groupId> <artifactId>webp-imageio</artifactId> <version>0.1.6</version> <scope> provided </scope> </dependency> </dependencies>

 



The problem is when I deploy my bundle (Project A) and run any service which is using a class (WebPWriteParam) from  webp-imageio-sejda I am facing an error 

 

 

java.lang.ClassCastException: class com.luciad.imageio.webp.WebPWriteParam cannot be cast to class com.luciad.imageio.webp.WebPWriteParam

 

 

This is because I have same JAR embedded in two different bundles. (Project A and Project B). I cannot Edit the POM of Project B.

Any idea How can I resolve this issue.

 

One solution I found that I can upload jar as an bundle and use that in both the bundles without embedding but I cannot edit the POM in the second bundle to remove embedding.

Thanks

 

@joerghoh @smacdonald2008 @arunpatidar @sham_hc @niall_o_donovan @lokesh_shivalingaiah 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

3 replies

Jagadeesh_Prakash
Community Advisor
Community Advisor
October 2, 2022

@vishnu_jangid Once try removing the embed from project A. Ideally the project A and B are getting deployed in the same instance so it should work. 

 

If this is not working let me know how you have embed the values and in which module did you do it

vishnu_jangid
October 2, 2022

Hello @jagadeesh_prakash,

 

Its not working because I have not exported the library package from project B. 

In core module I am added dependency with provided scope and added the library into <Embed-dependency>

you can check pom file above in description.

 

 

Thanks

Jagadeesh_Prakash
Community Advisor
Community Advisor
October 3, 2022

@vishnu_jangid  I did not get the point. Even if you add it in the core module it is on the same instance that it is getting installed right? 

 

I did this for one of the project and it worked for me, But i embeded it in "all" module. Please refer to below URL which gives you a clear idea

 

https://myaemlearnings.blogspot.com/2021/08/embedding-third-party-dependencyosgi.html

 

Shubham_borole
Community Advisor
Community Advisor
October 3, 2022

Hi @vishnu_jangid , wanted to check, in case you tried adding project B dependency in A, for core and all modules? Is project B an AEM project installed in same instance?

 

In all module, for a similar set up we have embedded project B as well

 

<embedded>
                            <groupId>com.project.b</groupId>
                            <artifactId>b.core</artifactId>
                            <type>jar</type>
                            <target>/apps/b-packages/application/install</target>
                        </embedded>
vishnu_jangid
October 3, 2022

Hello @shubham_borole Project B is an AEM project installed on same instance. Also if I include Project B in my project core module as JAR, I will not be able to access the embedded JAR(webp-imageio)'s classes.

Thanks

vishnu_jangid
October 3, 2022

Thanks @vishnu_jangid , have you by chance explored https://www.albinsblog.com/2017/11/javalangclasscastexception-for-same-class.html ?

 

I have done something similar for org.apache.commons.io and specified a version

 

<Export-Package> org.apache.commons.io.*;version=2.11.0</Export-Package>

Hello @shubham_borole I have tried <Export-Package>, <Private-Package>, <Embed-Dependency> almost everything but getting the same error everytime.

Thanks

vishnu_jangid
October 9, 2022

Hello all, Thanks for your positive response. I have found a solution for my issue.

 

The problem was in obtaining writer. The ServiceRegistry keep records of all service providers ie the implementations of ImageWriter class. In my case the WebPWriter is registered twice as I have embedded the JAR in two bundles, so when I am getting the writer as below I am getting two writers [I thought I will get one writer as the others will be not reachable as per OSGI concept], one which is loaded from classloader of Project A and one which is loaded from classloader of Project B.

 

When I use the writer loaded with classloader of Project B I am getting the exception.

 

// Obtaining a WebP ImageWriter instance
ImageWriter writer = ImageIO.getImageWritersByMIMEType("image/webp").next();


For resolving this issue, I have just changed the way of getting writer. I have iterated over list of writer and used writer which is loaded by my current bundle's classloader. 

// Obtain a WebP ImageWriter instance
ImageWriter webPWriter = null;
Iterator<ImageWriter> writers = ImageIO.getImageWritersByMIMEType("image/webp");

while(writers.hasNext()){
    ImageWriter tempWriter = writers.next();
    
    // MyClass is class in which I am writing this method
    if(tempWriter.getClass().getClassLoader() == MyClass.class.getClassLoader()){
        webPWriter = tempWriter;
        break;
    }
}

 


Please suggest if you have any better approach.
Thanks