Trying to reference an external jar through another external jar - httpcore through httpclient

StackOverflow https://stackoverflow.com/questions/19837934

  •  26-07-2022
  •  | 
  •  

Question

This all works fine when running junit in eclipse. It is when I deploy my jar to the Oracle Serviec Bus and call it via a proxy I get the problem.

My jar uses httpClient & httpCore, httpClient also has some dependencies on httpCore. The problem happens when httpclient references httpcore without being referenced from my jar. I have proven that is the external jar referencing the other one by editing the manifest of httpclient to have the httpcore jar. All works if I do this but I do not want to be manually editing external jars as you would expect!

My manifest has this class-path set: Class-Path: httpcore-4.3.jar httpclient-4.3.1.jar DatabaseUtil-1.0.jar commons-codec-1.8.jar

I am working on adding the jars to the classpath of the server and I think this will work but checking is there another way to do this as there are other services/jars using the same jvm. I have copied a test class below to show proof of issue

Any help is much appreciated.

Thanks, Daniel.

public class CheckHTTPJars {

  public static String checkHttpJarsLoaded()
  {

    try {
        System.out.println("Starting...");
        System.out.println("classpath: " + System.getProperty("java.class.path"));

        HttpHost host = new HttpHost("test");

        System.out.println("Hit HTTPCore... ");

        BasicResponseHandler b = new BasicResponseHandler();

        System.out.println("Hit HTTPClient... ");

        HttpGet get = new HttpGet();

        System.out.println("Hit HTTPClient but referencing HttpCore... ");

        HttpRequest h = null;

        System.out.println("Hit HTTPCore... ");


        return("Passed");
    } catch (Exception e) {
        return (e.getMessage());
    }

  } 
}
Was it helpful?

Solution 2

In the end I stuck with Ant and used the zipgroupfileset tag. This bundles the external jar classes into your jar. This is a bit messy as it is extracting the classes and copying them into your jar instead of bundling the jars in. I'm sure there is a way to bundle the jars and possibly the maven solution from arkonautom (Thanks by the way) will do this for you (I ran out of time so never got around to trying it).

Other solutions that I came aross - Copy the jars to the $domain/lib folder. This also worked but found I had to deploy the jars into the folder also or I was having trouble with my java callout not seeing the code. I didnt spend any time investigating - rewrote the code using java.net URLConnection. This also worked but was having trouble with authorisation when running locally so decided against it in the end.

Hope this helps someone else...

        <zipgroupfileset dir="lib">
              <include name="httpclient-4.3.1.jar"/>
              <include name="httpcore-4.3.jar"/>
              <include name="commons-codec-1.8.jar"/>
        </zipgroupfileset> 

OTHER TIPS

  1. Are you using OSB java callout activity?

  2. Do you get Class not found (or other exception(s))?

  3. What is the DatabaseUtil-1.0.jar, is it the one made by you and used in callout?

I assume answer to these questions is true in this answer.

You need both (unmodified) jars for your code to work.

If you don't want to place both jars in the server classpath (recommended by Oracle for common artifacts) you can bundle those with custom jar used in callout. This could be ok (depends on the usecase) seeing as both jars total to ~1MB. See How can I create an executable JAR with dependencies using Maven?

Rememeber that you need commons-codec and some other dependencies for these two, check http://mvnrepository.com/ for full list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top