gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:39:48 25 4
gpt4 key购买 nike

我使用 Spring 编写了一个简单的 hello world 应用程序,并使用 ANT 脚本为 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

最佳答案

在您的 jar 中目标,尝试basedir="build/classes"而不仅仅是"build" ,使其匹配 destdircompile目标。

看起来 JAR 是从应有的位置上方的一个目录构建的,因此类的内部路径不正确,类加载器无法找到它们。

关于java - 执行使用ANT编写的JAR时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7455321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com