执行使用ANT编写的JAR时出现问题

时间:2011-09-17 14:03:32

标签: java ant

我使用Spring和ANT脚本编写了一个简单的hello world应用程序,为hello world应用程序生成JAR文件。生成JAR并正确编译类。然后我将JAR复制到我的tomcat webapps文件夹中,启动tomcat并加载index.jsp页面。当我尝试从index.jsp页面导航时,我在JAR中的Servlet没有被重新识别。抛出下面给出的异常。

javax.servlet.ServletException: Wrapper cannot find servlet class my.hello.servlet.WelcomeServlet or a class it depends on
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    java.lang.Thread.run(Unknown Source)
root cause

java.lang.ClassNotFoundException: my.hello.servlet.WelcomeServlet
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    java.lang.Thread.run(Unknown Source)

有人可以告诉我为什么我的Servlet类没有得到重新认证吗?

我的目录结构是

MyFirstApp
    |
     --WEB_INF
         |
          -- lib
              |
               -- all external jars and the jar containing the class files of my app
         |
          -- web.xml
     |
      -- index.jsp
     |
      -- welcome.jsp

我的build.xml是

<project name="MyFirstApp" default="jar" basedir="..">  
    <property name="src" location="/shil/JAVA/Spring Workspace/myfirstapp1/src"/>
    <property name="build" location="build"/>
    <property name="lib" location="/shil/JAVA/Spring Workspace/myfirstapp1/WebContent/WEB-INF/lib"/>        
    <path id="classpath-example">   
        <fileset dir="${lib}" includes="*.jar"/>
    </path>     
    <target name="clean">
        <delete dir="${build}"/>
    </target>
    <target name="init" depends="clean">
        <echo>Creating the build directory</echo>
        <!---<mkdir dir="build/jar"/>
        <mkdir dir="build/my/hello"/>                   
        <mkdir dir="build/my/hello/servlet"/>-->    
        <mkdir dir="build/classes"/>    
    </target>
    <target name="compile" depends="init">
        <echo>compiling</echo>
        <!---<mkdir dir="build/classes"/>-->        
        <javac srcdir="${src}" destdir="build/classes"              includeantruntime="false">
            <classpath refid="classpath-example"/>
        </javac>
    </target>   
    <target name="jar" depends="compile">
        <echo>building the jar</echo>       
        <jar destfile="F:/shil/JAVA/Spring Workspace/myfirstapp1/ant/helloworld.jar"                            basedir="build"/>           
    </target>
    <target name="run" depends="jar">
        <echo>running the jar</echo>
        <java jar="F:/shil/JAVA/Spring Workspace/myfirstapp1/ant/helloworld.jar" fork="true"/>
    </target>
</project>

我尝试使用eclipse创建JAR。这也会产生错误。

JAR creation failed. See details for additional information.
  Exported with compile warnings: myfirstapp1/src/my/hello/HelloWorldApp.java
  Could not find source file attribute for: 'F:\shil\JAVA\Spring Workspace\myfirstapp1\build\classes\my\hello\HelloWorldApp.class'
  Source name not found in a class file - exported all class files in myfirstapp1/build/classes/my/hello
  Resource is out of sync with the file system: '/myfirstapp1/build/classes/my/hello/HelloWorldApp.class'.
  Resource is out of sync with the file system: '/myfirstapp1/build/classes/my/hello/MyTime.class'.
  Resource is out of sync with the file system: '/myfirstapp1/build/classes/my/hello/SayHello.class'.
  Could not find source file attribute for: 'F:\shil\JAVA\Spring Workspace\myfirstapp1\build\classes\my\hello\servlet\HelloWorldServlet.class'
  Source name not found in a class file - exported all class files in myfirstapp1/build/classes/my/hello/servlet
  Resource is out of sync with the file system: '/myfirstapp1/build/classes/my/hello/servlet/HelloWorldServlet.class'.
  Resource is out of sync with the file system: '/myfirstapp1/build/classes/my/hello/servlet/WelcomeServlet.class'.
有人可以帮忙吗?

这是jar的结构。

helloworld.jar
     |
      -- META-INF
            |
             -- MANIFEST.MF
     |
      -- my
          |
           -- hello
                |
                 -- HelloWorldApp.class 
                |
                 -- MyTime.class
                |
                 -- SayHello.class
                |
                 -- servlet
                       |
                        -- HelloWorldServlet.class
                       | 
                        -- WelcomeServlet.class

2 个答案:

答案 0 :(得分:0)

jar目标中,请尝试basedir="build/classes"而非"build",以使其与destdir目标中的compile匹配。

看起来JAR是从它上面的一个目录构建的,所以类的内部路径不正确,类加载器找不到它们。

答案 1 :(得分:0)

在重新启动tomcat之前,请尝试删除{tomcat_home} / work文件夹中的文件夹。