gpt4 book ai didi

java - 构建时找不到符号 request.getServletContext()

转载 作者:太空宇宙 更新时间:2023-11-04 09:16:06 25 4
gpt4 key购买 nike

我一直在编写一个脚本来构建项目、创建 War 文件并将其部署到 Tomcat 上。我在执行脚本时收到以下错误,特别是在 javac 命令期间。请注意,我尝试在没有 Eclipse 的情况下进行此操作,因此无法直接集成 Tomcat 7。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:ant="antlib:net.sf.antcontrib" name="Build Script For Deploy" default="NN-build" >
   
   <!-- This sript will read required proeprty values from below properties file -->
   <property file="upgrade.properties" />
   
   <target name="NN-build" depends="compile" description="NN VM build target with CDT update" />
   
   <!--Keeping all the required jar files to classpath.
   All the ANT tasks used in the rest of the script will be executed from below jar files Hence they need to be on classpth. -->
   <path id="svnant.classpath">
  <fileset dir= "${lib.dir}" >
<include name= "**/*.jar" />
</fileset>
    </path>  

<path id = "catalina-ant-classpath">
<fileset dir="${appserver.lib}">
<include name = "catalina-ant.jar" />
</fileset>
</path>

<taskdef name="install" classname = "org.apache.catalina.ant.DeployTask">
<classpath refid = "catalina-ant-classpath" />
</taskdef>

   <!-- Defining all the required ANT tasks which will be used in the rest of the script -->
    <typedef resource="org/tigris/subversion/svnant/svnantlib.xml" classpathref="svnant.classpath" />
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="${lib.dir}/ant-contrib-1.0.jar"/>
</classpath>
</taskdef>
   

  <property name="src.dir" location="${dest.home}/src" />
  <property name="classes.dir" location="${dest.home}/build/classes"/>
  <property name="dist.dir" location="${dest.home}/WebContent/WarFile" />
  <path id="compile.classpath">
<fileset dir="${dest.home}/WebContent/WEB-INF/lib">
<include name="*.jar" />
</fileset>
  </path>

    <!-- Target for deleting the existing directories-->
  <target name="clean">
    <delete dir="${classes.dir}" />
  <delete dir="${dist.dir}" />
  </target>
 
  <!-- Target for creating the new directories-->
  <echo>....Creating new directories.... </echo>
  <target name="makedir">
    <mkdir dir="${classes.dir}" />
  <mkdir dir="${dist.dir}" />
  </target>

<!-- Target for compiling the java code-->
  <target name="compile" depends="clean, makedir">
    <javac destdir="${classes.dir}" debug="true"
deprecation="false" optimize = "false" failonerror = "true" >
<src path="${src.dir}" />
<classpath refid="compile.classpath" />
</javac>
  </target>
 
</project>

java:43: error: cannot find symbol ServletContext context = request.getServletContext();    [javac]                                         ^
   
[javac]   symbol:   method getServletContext()
[javac]   location: variable request of type HttpServletRequest
[javac] Note: Some input files use unchecked or unsafe operations.
[javac] Note: Recompile with -Xlint:unchecked for details.
[javac] 1 error

BUILD FAILED
C:\task\task-upgrade.xml:97: Compile failed; see the compiler error output for
 details.

当我在 Eclipse 中导入项目时,我收到了同样的错误,但添加服务器运行时作为 Tomcat7 修复了它。

最佳答案

构建 J2EE 应用程序时,用于编译的类路径需要与运行时使用的类路径不同。由于您的 Web 应用程序使用 J2EE API,因此需要针对它进行编译。但是一旦部署,这些 API 将由您选择的容器(此处为 Tomcat)提供,因此它们不应该成为您的 Web 应用程序的一部分。

因此,在您的具体示例中,您需要 javax servlet-api。它的 jar 需要添加到您的编译类路径中,但不要放入 WEB-INF/lib 文件夹中。

以下是可能适合您需要的 jar 列表:

https://search.maven.org/search?q=g:javax.servlet%20AND%20a:javax.servlet-api&core=gav

https://search.maven.org/search?q=g:org.apache.tomcat%20AND%20a:tomcat-servlet-api&core=gav

关于java - 构建时找不到符号 request.getServletContext(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58954456/

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