gpt4 book ai didi

java - 在 ant 脚本中包含外部 JAR 时出错

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:55:34 24 4
gpt4 key购买 nike

这是我第一次尝试编写 ANT 脚本。这是我使用 Spring 的简单 Hello World 应用程序的 build.xml。

<project name="MyFirstApp" default="jar" basedir="..">  
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="lib" location="/WebContent/WEB-INF/lib"/>
<path id="classpath-example">
<fileset dir="${build}" includes="*.jar"/>
</path>
<target name="clean">
<delete dir="build"/>
</target>
<target name="compile">
<mkdir dir="build/classes"/>
<javac srcdir="${src}" destdir="build/classes" includeantruntime="false"/>
<classpath refid="classpath-example"/>
</target>
<target name="jar">
<mkdir dir="build/jar"/>
<jar destfile="build/jar/MyFirstApp.jar" basedir="build/classes"/>
</target>
<target name="run">
<java jar="build/jar/MyFirstApp.jar" fork="true"/>
</target>
</project>

当我尝试运行它时,会显示以下错误消息。

问题:无法创建任务或键入类路径原因:名称未定义。行动:检查拼写。操作:检查是否已声明任何自定义任务/类型。行动:检查任何/声明是否已经发生。

有人可以告诉我要做什么吗?

我第一次运行 ANT 脚本时,生成了 JAR 文件。当我再次尝试运行它时,它给出了下面提到的错误。

清洁: [delete] 删除目录F:\shil\JAVA\Spring Workspace\myfirstapp1\build

compile:
[echo] compiling
[mkdir] Created dir: F:\shil\JAVA\Spring Workspace\myfirstapp1\build\classes

[javac] Compiling 5 source files to F:\shil\JAVA\Spring Workspace\myfirstapp
1\build\classes
[javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Hello
WorldServlet.java:4: package javax.servlet does not exist
[javac] import javax.servlet.ServletException;
[javac] ^
[javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Hello
WorldServlet.java:5: package javax.servlet.http does not exist
[javac] import javax.servlet.http.HttpServlet;
[javac] ^
[javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Hello
WorldServlet.java:6: package javax.servlet.http does not exist
[javac] import javax.servlet.http.HttpServletRequest;
[javac] ^
[javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Hello
WorldServlet.java:7: package javax.servlet.http does not exist
[javac] import javax.servlet.http.HttpServletResponse;
[javac] ^
[javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Hello
WorldServlet.java:12: cannot find symbol
[javac] symbol: class HttpServlet
[javac] public class HelloWorldServlet extends HttpServlet {
[javac] ^
[javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Hello
WorldServlet.java:26: cannot find symbol
[javac] symbol : class HttpServletRequest
[javac] location: class my.hello.servlet.HelloWorldServlet
[javac] protected void doGet(HttpServletRequest request, HttpServletResp
onse response) throws ServletException, IOException {
[javac] ^
[javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Hello
WorldServlet.java:26: cannot find symbol
[javac] symbol : class HttpServletResponse
[javac] location: class my.hello.servlet.HelloWorldServlet
[javac] protected void doGet(HttpServletRequest request, HttpServletResp
onse response) throws ServletException, IOException {
[javac] ^
[javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Hello
WorldServlet.java:26: cannot find symbol
[javac] symbol : class ServletException
[javac] location: class my.hello.servlet.HelloWorldServlet
[javac] protected void doGet(HttpServletRequest request, HttpServletResp
onse response) throws ServletException, IOException {
[javac]
^
[javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Hello
WorldServlet.java:33: cannot find symbol
[javac] symbol : class HttpServletRequest
[javac] location: class my.hello.servlet.HelloWorldServlet
[javac] protected void doPost(HttpServletRequest request, HttpServletRes
ponse response) throws ServletException, IOException {
[javac] ^
[javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Hello
WorldServlet.java:33: cannot find symbol
[javac] symbol : class HttpServletResponse
[javac] location: class my.hello.servlet.HelloWorldServlet
[javac] protected void doPost(HttpServletRequest request, HttpServletRes
ponse response) throws ServletException, IOException {
[javac] ^
[javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Hello
WorldServlet.java:33: cannot find symbol
[javac] symbol : class ServletException
[javac] location: class my.hello.servlet.HelloWorldServlet
[javac] protected void doPost(HttpServletRequest request, HttpServletRes
ponse response) throws ServletException, IOException {
[javac]
^
[javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Welco
meServlet.java:4: package javax.servlet does not exist
[javac] import javax.servlet.RequestDispatcher;
[javac] ^
[javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Welco
meServlet.java:5: package javax.servlet does not exist
[javac] import javax.servlet.ServletException;
[javac] ^
[javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Welco
meServlet.java:6: package javax.servlet.http does not exist
[javac] import javax.servlet.http.HttpServlet;
[javac] ^
[javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Welco
meServlet.java:7: package javax.servlet.http does not exist
[javac] import javax.servlet.http.HttpServletRequest;
[javac] ^
[javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Welco
meServlet.java:8: package javax.servlet.http does not exist
[javac] import javax.servlet.http.HttpServletResponse;
[javac] ^
[javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Welco
meServlet.java:15: cannot find symbol
[javac] symbol: class HttpServlet
[javac] public class WelcomeServlet extends HttpServlet {
[javac] ^
[javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Welco
meServlet.java:29: cannot find symbol
[javac] symbol : class HttpServletRequest
[javac] location: class my.hello.servlet.WelcomeServlet
[javac] protected void doGet(HttpServletRequest request, HttpServletResp
onse response) throws ServletException, IOException {
[javac] ^
[javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Welco
meServlet.java:29: cannot find symbol
[javac] symbol : class HttpServletResponse
[javac] location: class my.hello.servlet.WelcomeServlet
[javac] protected void doGet(HttpServletRequest request, HttpServletResp
onse response) throws ServletException, IOException {
[javac] ^
[javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Welco
meServlet.java:29: cannot find symbol
[javac] symbol : class ServletException
[javac] location: class my.hello.servlet.WelcomeServlet
[javac] protected void doGet(HttpServletRequest request, HttpServletResp
onse response) throws ServletException, IOException {
[javac]
^
[javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Welco
meServlet.java:36: cannot find symbol
[javac] symbol : class HttpServletRequest
[javac] location: class my.hello.servlet.WelcomeServlet
[javac] protected void doPost(HttpServletRequest request, HttpServletRes
ponse response) throws ServletException, IOException {
[javac] ^
[javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Welco
meServlet.java:36: cannot find symbol
[javac] symbol : class HttpServletResponse
[javac] location: class my.hello.servlet.WelcomeServlet
[javac] protected void doPost(HttpServletRequest request, HttpServletRes
ponse response) throws ServletException, IOException {
[javac] ^
[javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Welco
meServlet.java:36: cannot find symbol
[javac] symbol : class ServletException
[javac] location: class my.hello.servlet.WelcomeServlet
[javac] protected void doPost(HttpServletRequest request, HttpServletRes
ponse response) throws ServletException, IOException {
[javac]
^
[javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Welco
meServlet.java:41: cannot find symbol
[javac] symbol : class RequestDispatcher
[javac] location: class my.hello.servlet.WelcomeServlet
[javac] RequestDispatcher welcomeDispatcher = getServletContext(
).getRequestDispatcher("/Welcome.jsp");
[javac] ^
[javac] F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\servlet\Welco
meServlet.java:41: cannot find symbol
[javac] symbol : method getServletContext()
[javac] location: class my.hello.servlet.WelcomeServlet
[javac] RequestDispatcher welcomeDispatcher = getServletContext(
).getRequestDispatcher("/Welcome.jsp");
[javac] ^
[javac] Note: F:\shil\JAVA\Spring Workspace\myfirstapp1\src\my\hello\HelloWo
rldApp.java uses or overrides a deprecated API.
[javac] Note: Recompile with -Xlint:deprecation for details.
[javac] 25 errors

BUILD FAILED
F:\shil\JAVA\Spring Workspace\myfirstapp1\ant\build.xml:19: Compile failed; see
the compiler error output for details.

Total time: 1 second

为什么这不是第一次发生?有人可以帮忙吗?

最佳答案

<classpath>在你的里面compile目标应该嵌套在 <javac> 中元素。

还值得注意的是,ant(事实上,大多数构建系统)的很多功能都来自于声明目标之间的依赖关系。在您的示例中,运行 jar 没有任何意义。没有先运行 compile 的目标目标,否则,没有要 jar 的类文件 - 或者更糟的是,您将运行 jar手动,但忘记运行 compile首先并以 JAR 文件中的过时类结束。所以,jar目标应该取决于 compile目标。这样,无论何时运行 jar目标,任何更改的类将首先自动编译。同样,run目标应该取决于 jar目标。

关于java - 在 ant 脚本中包含外部 JAR 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7453652/

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