尝试加载HttpUriRequest时,Websphere上的java.lang.LinkageError

时间:2014-11-06 10:08:00

标签: java maven websphere linkageerror

我在项目中使用CUPS4J,这取决于http-client,http-core和slf4j。

要解决依赖关系,我们使用Maven,并且我已经定义了依赖关系,如下所示:

<dependency>
    <groupId>cups4j</groupId>
    <artifactId>cups4j</artifactId>
    <version>0.6.4</version>
</dependency>

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.0.3</version>
</dependency>

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.1</version>
</dependency>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.7</version>
</dependency>

cups4j依赖是我们的ArtiFactory服务器(我无法在线找到它)。

如果我创建一个样本main方法来打印某个文档并将其作为java应用程序启动,那么一切都像魅力一样。

当我将我的类发布到Websphere服务器并从网页调用该方法时,它会生成java.lang.LinkageError

这是stacktrace的相关部分:

Caused by: java.lang.LinkageError: loader constraint violation: loader "org/eclipse/osgi/internal/baseadaptor/DefaultClassLoader@208c132" previously initiated loading for a different type with name "org/apache/http/client/methods/HttpUriRequest" defined by loader "com/ibm/ws/classloader/CompoundClassLoader@1e0f797"
    at java.lang.ClassLoader.defineClassImpl(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:260)
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.defineClass(DefaultClassLoader.java:188)
    at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.defineClass(ClasspathManager.java:580)
    at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.findClassImpl(ClasspathManager.java:550)
    at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.findLocalClassImpl(ClasspathManager.java:481)
    at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.findLocalClass_LockClassName(ClasspathManager.java:460)
    at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.findLocalClass(ClasspathManager.java:447)
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.findLocalClass(DefaultClassLoader.java:216)
    at org.eclipse.osgi.internal.loader.BundleLoader.findLocalClass(BundleLoader.java:393)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:469)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:422)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:410)
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:612)
    at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:584)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:708)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:700)
    at org.cups4j.operations.IppOperation.sendRequest(IppOperation.java:207)
    at org.cups4j.operations.IppOperation.request(IppOperation.java:76)
    at org.cups4j.CupsPrinter.print(CupsPrinter.java:113)
    at it.dropcomp.tasks.print.PrinterService.printPDF(PrinterService.java:160)

这是打印PDF(内部it.dropcomp.tasks.print.PrinterService)的方法:

public void printPDF() throws RemoteServiceException {
    /* 
     * generatedPDF is defined as File, and it's properly initialized 
     * before calling this method.
     */
    if(generatedPDF == null) {
        throw new RemoteServiceException("You must generate a file first!");
    }
    try {
        CupsPrinter selectedPrinter = new CupsPrinter(
            new URL(Constants.PRINTER_FULL_URL), 
            Constants.PRINTER_NAME, true
        );
        InputStream is = new FileInputStream(generatedPDF);
        PrintJob pj = new PrintJob.Builder(is).build();
        selectedPrinter.print(pj); //this is line 160
    } catch (Exception e) {
        LOG.error("Exception", e);
        throw new RemoteServiceException(e);
    }
}

似乎HttpUriRequest已经存在并且与Apache的httpclient库提供的那个冲突,但如果我尝试从pom.xml删除该依赖项,我会得到NoClassDefFoundException 1}}为那个班。

如果重要,我的IDE就是Eclipse Luna。

如何解决此异常?

1 个答案:

答案 0 :(得分:0)

WebSphere也使用httpclient库,这可能与您提供的库冲突。 尝试通过Environment > Shared Libraries在管理控制台中创建独立的共享库。将http-*slf4jcups4j个jar放在那里,并将该共享库与您的应用程序相关联。