简体   繁体   中英

Ant <javac> fails due to “package javax.servlet does not exist”

I'm trying to compile AdminOps.java file which is a part of the package, adminOps

Here is the file structure:

C:\\Users\\Hp\\git\\fls-web-Lucy\\src\\adminOps\\AdminOps.java

According to the Ant output, I think Ant cannot find javax.servlet

I tried importing the javax.servlet-api-3.1.0.jar file manually but still I'm getting error. I'm using a build.xml file so that i can create a WAR file and deploy it on localhost using XAMPP.

Could anyone help me find out what's wrong?

Ant Script

<?xml version="1.0" encoding="UTF-8"?>
<project name="friendlease" default="main" basedir=".">
    <property name="src.java" value="${basedir}/src/"/>
    <property name="build.dir" value="${basedir}/build"/>
    <property name="jar.dir" value="${build.dir}/jar"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
    <property name="lib.dir" value="${basedir}/lib"/>
    <property name="tp.dir" value="${jar.dir}/thirdparty"/>
    <property name="jar.name" value="${ant.project.name}.jar"/>
    <property name="jar.file" value="${jar.dir}/${jar.name}"/>
    <property name="war.file" value="${build.dir}/${ant.project.name}.war"/>

    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

    <target name="setup">
        <mkdir dir="${build.dir}"/>
        <mkdir dir="${jar.dir}"/>
        <!--
        <for list="${dependencies}" trim="true" param="dependency">
            <sequential>
                <echo message="___ Building dependency @{dependency} ___"/>
                <ant antfile="@{dependency}/build.xml" inheritall="false"
                    target="jar"/>
                <copy todir="${jar.dir}">
                    <fileset dir="@{dependency}/${jar.dir}"/>
                </copy>
            </sequential>
        </for>
        -->
        <copy todir="${tp.dir}/">
            <fileset dir="${lib.dir}"/>
        </copy>
        <path id="classpath">
            <fileset dir="${jar.dir}" includes="**/*.jar"/>
        </path>
    </target>

    <target name="compile" depends="setup">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.java}" destdir="${classes.dir}"
            classpathref="classpath" includeantruntime="false"
            debug="${compile.debug}" debuglevel="lines,vars,source"/>
    </target>

    <target name="test-compile" depends="compile">
        <mkdir dir="${test.classes.dir}"/>
        <path id="test.compile.classpath">
            <pathelement path="${classes.dir}"/>
            <pathelement location="${test.testng.jar}"/>
            <path refid="classpath"/>
        </path>
        <javac srcdir="${test.src.java}" destdir="${test.classes.dir}"
            classpathref="test.compile.classpath" includeantruntime="false"/>
    </target>

    <target name="test-run" depends="test-compile">
        <path id="test.run.classpath">
            <pathelement path="${test.classes.dir}"/>
            <path refid="test.compile.classpath"/>
        </path>
        <testng classpathref="test.run.classpath"
            outputdir="${test.report.dir}" haltonfailure="true">
            <classfileset dir="${test.classes.dir}" includes="**/*.class"/>
        </testng>
    </target>

    <target name="jar" depends="compile">
        <pathconvert property="runtime.classpath" refid="classpath">
            <regexpmapper from=".+/jar/(.+)" to="\1"/>
        </pathconvert>
        <manifestclasspath property="manifest.classpath"
            jarfile="${broken.on.purpose}">
            <classpath>
                <pathelement path="${runtime.classpath}"/>
            </classpath>
        </manifestclasspath>
        <jar destfile="${jar.file}" basedir="${classes.dir}">
            <manifest>
                <attribute name="Build-Version" value="${build.version}"/>
                <attribute name="Class-Path" value="${manifest.classpath}"/>
            </manifest>
        </jar>
    </target>

    <target name="war" depends="jar">
        <war destfile="${war.file}"
            webxml="${basedir}/WebContent/WEB-INF/web.xml">
            <lib dir="${jar.dir}" includes="*.jar"/>
            <lib dir="${tp.dir}" includes="*.jar">
                <exclude name="servlet-api.jar"/>
                <exclude name="javax.servlet-3.0.jar"/>
            </lib>
            <zipfileset dir="${basedir}/WebContent"
        excludes="WEB-INF/web*.xml"
        />
        </war>
    </target>

    <target name="main" depends="clean, war"/>
</project>

Environment Variables

ANT_HOME=C:\apache-ant-1.9.6
Path=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Skype\Phone\;%ANT_HOME%\bin;%JAVA_HOME%\bin

AdminOps.java

package adminOps;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AdminOps extends HttpServlet {
//some code here...
}

Ant Output

Buildfile: C:\Users\Hp\git\fls-web-Lucy\build.xml
clean:
    [delete] Deleting directory C:\Users\Hp\git\fls-web-Lucy\build
setup:
    [mkdir] Created dir: C:\Users\Hp\git\fls-web-Lucy\build
    [mkdir] Created dir: C:\Users\Hp\git\fls-web-Lucy\build\jar
    [copy] Copying 1 files to C:\Users\Hp\git\fls-web-Lucy\build\jar\thirdparty
compile:
    [mkdir] Created dir: C:\Users\Hp\git\fls-web-Lucy\build\classes
    [javac] Compiling 1 source files to C:\Users\Hp\git\fls-web-Lucy\build\classes
    [javac] C:\Users\Hp\git\fls-web-Lucy\src\adminOps\AdminOps.java:7: error: package javax.servlet does not exist
    [javac] import javax.servlet.ServletException;
    [javac]                     ^
    [javac] C:\Users\Hp\git\fls-web-Lucy\src\adminOps\AdminOps.java:8: error: package javax.servlet.annotation does not exist
    [javac] import javax.servlet.annotation.WebServlet;
    [javac]                                ^
                                                        ^
    [javac]   location: class AdminOps
    [javac] 2 errors

BUILD FAILED
C:\Users\Hp\git\fls-web-Lucy\build.xml:43: Compile failed; see the compiler error output for details.

Since Tomcat is a part of XAMPP I have not installed it separately. Is this creating problems?

In your Ant script, the snippet...

<javac ... classpathref="classpath" ... />

...configures <javac> to search for libraries in the paths defined by classpath . In your script, classpath is defined as...

<path id="classpath">
    <fileset dir="${jar.dir}" includes="**/*.jar"/>
</path>

So either:

  • javax.servlet-api-3.1.0.jar needs to exist under ${jar.dir} .
  • or <path id="classpath"> needs another <fileset> pointing to javax.servlet-api-3.1.0.jar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM