GlassFish 3.1.2.2 war can't find jar inside ea

2019-06-12 17:18发布

问题:

I have built an ear with this structure (not all files shown here):

myapp.ear/
    myapp-ejb.jar
    myapp-web.war
    META-INF/
        application.xml
    lib
        myapp-common.jar

The problem is, when code in the war tries to reference classes in myapp-common.jar, it throws java.lang.ClassNotFoundException.

Note the contents of META-INF/application.xml:

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                                 http://java.sun.com/xml/ns/javaee/application_6.xsd"
             version="6">
  <display-name>pncr-portal-ear</display-name>
  <module>
    <web>
      <web-uri>myapp-web.war</web-uri>
      <context-root>/</context-root>
    </web>
  </module>
  <module>
    <ejb>myapp-ejb.jar</ejb>
  </module>
  <library-directory>/lib</library-directory>
</application>

The library directory is defined here as being in the /lib directory relative to the root of the ear, which is exactly where it is, yet it is not loaded into the classpath despite the Java EE 5 (and presumably 6) spec saying that it should be loaded into the classpath.

回答1:

As it turns out, I was referencing a class in a slightly different package than the one I thought. The package no longer existed, yet Maven was compiling it without complaining. So I blew away my local repository and tried again. I still got the error. Then I found the problem: In myapp.taglib.xml, I am still referencing an old version of a class in myapp-common.jar that is now in a different package.

So the lesson is: If you see ClassNotFoundException, also look in your taglib.xml file(s).



回答2:

Verify that you have META-INF/manifest.mf in your war file, containing (something like):

Manifest-Version: 1.0
Class-Path: lib/myapp-common.jar