How do you resolve java LinkageError issues when writing your own confluence plugin?

P August 10, 2020

I am following steps similar to this tutorial : 

https://developer.atlassian.com/server/framework/atlassian-sdk/create-a-confluence-hello-world-macro/ 

Except instead of creating a Hello World macro, I decided to create a Hello Word Action using "ConfluenceActionSupport".  It looks something like this: 

myUserInfoAction.java : 

package hello;



import com.atlassian.confluence.core.ConfluenceActionSupport;
import com.atlassian.confluence.user.AuthenticatedUserThreadLocal;
import java.io.IOException;import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONException;



public class myUserInfoAction extends ConfluenceActionSupport  {

      String userName = AuthenticatedUserThreadLocal.getUserName();
      System.out.println("User logged in is: " + userName); // <-- This works correctly (no problems)
 
 String url = "http://localhost/myWebService/getUserInfo/" + username; 



   try {

       JSONArray json; 

       json = getJSONArrayObject(url); // <--- just a function that returns a JSONArray obj

           System.out.println(json.getJSONObject(0).get("userInfo").toString()); // <-- breaks here, look at the error message below. Strange thing is if I create a standalone executable JAR in NetBeans, this line works perfectly (i.e. it prints out user information).

         }
}

 

javax.servlet.ServletException : servlet execuation threw an exception at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java : 315)

caused by: java.lang.LinkageError : loader constraint violation : loader(instance of org/apache/felix/framework/BundleWiringImpl$BundleClassLoader Java5)
previously initiated loading for a different type with name "org/json/JSONArray" at hello.myUserInfoAction.execute(myUserInfoAction.java : 35)

 

My suspicion is this is a classloader problem.  After a bit of research, it appears the directory located at 

atlassian-confluence-6.2.0/confluence/WEB-INF/classes/atlassian-bundled-plugins contains a JAR file called "json-20070829-1.jar"

at the same time, the directory located at 

atlassian-confluence-6.2.0/confluence/WEB-INF/lib contains a JAR file called "json-20070829.jar".  

There are clearly two JAR files being loaded with a class "org.json.JSONArray".  It turns out these two JAR files are actually different (i.e. I used "diff" command to check and they do differ).  

My code (as shown above) doesn't really care which JAR file to use, but just to pick one of the JAR files and move on.  How do I achieve this? All I want to do in my simple plugin is print the contents of that JSONArray object. 

0 answers

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
6.2.0
TAGS
AUG Leaders

Atlassian Community Events