2011-09-17 87 views
1

我已經使用Spring和ANT腳本編寫了一個簡單的Hello World應用程序,用於爲Hello World應用程序生成JAR文件。 JAR正在生成,並且類正在被正確編譯。然後我將JAR複製到我的tomcat webapps文件夾中,啓動tomcat並加載了index.jsp頁面。當我試圖從index.jsp頁面導航時,JAR中的我的Servlet沒有得到識別。它會拋出下面給出的異常。執行使用ANT編寫的JAR時出現問題

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'. 

有人可以幫忙嗎?

這是罐子的結構。

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

如果在某個文件夾中沒有額外的類包裝,請查看jar結構。 – Santosh

回答

0

在你jar目標,嘗試basedir="build/classes",而不是僅僅"build",使其在compile目標destdir匹配。

它看起來像JAR被從一個目錄生成以上地方應該是,這樣的類內部的路徑是不正確的類加載器無法找到他們。

+0

我想這樣做,太..它仍然沒有工作:( – meenakshi

0

也請您嘗試重新啓動Tomcat之前刪除{} TOMCAT_HOME /工作文件夾內的文件夾。

+0

{} TOMCAT_HOME /工作文件夾爲空,它仍然不工作! – meenakshi

+0

你能解壓的罐子,看看類正確組織的? – Santosh