7 Replies Latest reply on Aug 23, 2010 11:51 AM by darrylburke

    The import javax.servlet cannot be resolved - What do I wrong?

      Hi colleagues,

      for the beginning I wrote a small HelloWorld Servlet in Eclipse but the compiler
      tells: The import javax.servlet cannot be resolved.

      For using J2EE I installed Eclipse 3.0.1, j2sdk1.4.2_07, Sun Java System Application Server Platform Edition 8.1 and J2EE 1.4 SDK Samples and I add the path for J2EE in Eclipse by navigating Window -> Preferences -> Java -> Build Path -> Classpath Variables and adding J2EE_HOME d:\Progs\Sun\AppServer.

      What did I wrong? Or must I install something else so that it works (e.g. some plugin)?

      Thank you for answering me.
        • 1. Re: The import javax.servlet cannot be resolved - What do I wrong?
          For using J2EE I installed Eclipse 3.0.1,
          j2sdk1.4.2_07, Sun Java System Application Server
          Platform Edition 8.1 and J2EE 1.4 SDK Samples and I
          add the path for J2EE in Eclipse by navigating
          Window -> Preferences -> Java -> Build Path ->
          Classpath Variables and adding J2EE_HOME
          Add j2ee.jar from from %J2EE_HOME%\lib as an external JAR to the project. It should not have any qualms compiling then. :-)
          • 2. Re: The import javax.servlet cannot be resolved - What do I wrong?

            I don't know how I can compile my servlet in Eclipse. Calling Run on the project opens a window where I can create new configurations for a Java Applet, a Java application or a wireless Toolkit Emulator, but no possibility to run a Java servlet.

            How do you compile a Java servlet in Eclipse?

            From desperation I tried to compile my servlet in Windows console by calling but I get the messages that the packages I need do not exist:

            D:\Progs\eclipse\WorkspaceServlets> javac -classpath %J2EE_HOME%\lib\j2ee.jar Ch05\eb\Login.java
            Ch05\web\Login.java:9: package javax.servlet.http does not exist
            import javax.servlet.hhtp.*;
            and so on

            What's the matter?

            I only found out that I have no j2sdkee1.4 directory because I did not installed the J2EE All-In-One-Bundle because I had J2SE 1.4.02 installed. Instead of it I only installed the Sun Java System Application Server Platform Edition 8.1. I did not install the J2EE 1.4 Platform API Documentation but I think that's not necessary because it is only a documentation.

            What do I wrong?

            Best regards,
            • 3. i am putting source code in eclipse
              i am putting source code in eclipse , in that one servlet is there but eclipse but when i am running that code servlet is not resolving.
              • 4. Re: i am putting source code in eclipse
                I am trying to build a project in Eclipse 3.1. I installed j2sdk1.4.2_09. I am getting the following error:
                "The project was not built since its build path is incomplete. Cannot find the class file for javax.servlet.ServletRequest. Fix the build path then try building this project"
                My j2sdk1.4.2_09 is installed in my C:drive. I added the following to the classpath in Eclipse
                Why am I still getting the javax.servlet error?
                • 5. Re: i am putting source code in eclipse
                  You can use an external Ant file to build all your project, as:
                  ant -projecthelp
                  ant compile
                  ant build
                  ant deploy

                  Below you can find an example:

                  <project name="DTAC" default="compile" basedir=".">

                  <!-- ===================== Property Definitions =========================== -->

                  All properties should be defined in this section.
                       Any host-specific properties should be defined
                       in the build.properties file.

                       In this app, the following properties are defined in build.properties:

                       o tomcat.home - the home directory of your Tomcat installation
                       o webapps.home - the place to copy the war file to deploy it

                  <!-- <property file="build.properties" /> -->

                  <property name="app.home" value="." />
                  <property name="app.name" value="dtac" />
                  <property name="tomcat.home"          value="C:\apache-tomcat-6.0.14"/>
                  <property name="webapps.home"          value="C:\apache-tomcat-6.0.14\webapps"/>

                  <property name="src.home" value="${app.home}/src"/>
                  <property name="lib.home" value="${app.home}/lib"/>
                  <property name="deploy.home" value="${app.home}/deploy"/>
                  <property name="object.home" value="${app.home}/object"/>
                  <property name="doc.home" value="${app.home}/doc"/>
                  <property name="web.home" value="${app.home}/WebContent"/>

                  <property name="build.home" value="${app.home}/build"/>
                  <property name="build.classes" value="${build.home}/WEB-INF/classes"/>
                  <property name="build.lib" value="${build.home}/WEB-INF/lib"/>

                  <property name="src" location="src" />
                  <property name="classes" location="classes" />
                  <property name="include" location="classes" />

                  <!-- ==================== Compilation Classpath =========================== -->

                  This section creates the classpath for compilation.

                  <path id="compile.classpath">

                  <!-- The object files for this application -->
                  <pathelement location="${object.home}"/>

                  <!-- The lib files for this application -->
                  <fileset dir="${lib.home}">
                  <include name="*.jar"/>
                  <include name="*.zip"/>

                  <!-- All files/jars that Tomcat makes available -->
                  <!--fileset dir="${tomcat.home}/common/lib">
                  <include name="*.jar"/>
                       <include name="*.zip"/>
                  <pathelement location="${tomcat.home}/classes"/>
                  <!--<pathelement location="${tomcat.home}/common/classes"/>-->


                  <!-- ==================== Build Targets below here========================= -->

                  <!-- ==================== "help" Target =================================== -->

                  This is the default ant target executed if no target is specified.
                       This helps avoid users just typeing 'ant' and running a
                       default target that may not do what they are anticipating...

                  <target name="help" >
                  <echo message=" Favor de especificar un target! [uso: ant <targetname>]" />
                  <echo message=" List de posibles targets: "/>
                  <echo message=" clean-all.....Delete build dir, all .class and war files"/>
                  <echo message=" prepare.......Creates directories if required" />
                  <echo message=" compile.......Compiles source files" />
                  <echo message=" build.........Build war file from .class and other files"/>
                  <echo message=" deploy........Copy the app and Axis war files to Tomcat" />
                  <echo message=" javadoc.......Generates javadoc for this application" />

                  <!-- ==================== "clean-all" Target ============================== -->

                  This target should clean up any traces of the application
                       so that if you run a new build directly after cleaning, all
                       files will be replaced with what's current in source control

                  <target name="clean-all" >
                  <delete dir="${build.home}"/>
                  <delete dir="${object.home}"/>
                  <delete dir="${deploy.home}"/>

                  <!-- can't delete directory if Tomcat is running -->
                  <delete dir="${webapps.home}/${app.name}"/>

                  <!-- deleting the deployed .war file is fine even if Tomcat is running -->
                  <delete dir="${webapps.home}/${app.name}.war" />

                  <!-- delete the javadoc -->
                  <delete dir="${doc.home}"/>

                  <!-- delete all the class files from the source dir -->
                  <fileset dir="${src.home}" includes="**/*.class"/>

                  <!-- Note that the clean target doesn't delete the Axis server webapp -->
                  <!-- This is since other apps may be using it - add it here if needed -->


                  <!-- ==================== "prepare" Target ================================ -->

                  This target is executed prior to any of the later targets
                       to make sure the directories exist. It only creates them
                       if they need to be created....
                       Other, similar, preparation steps can be placed here.

                  <target name="prepare">

                  <echo message="Tomcat Home = ${tomcat.home}" />
                  <echo message="webapps Home = ${webapps.home}" />

                  <mkdir dir="${object.home}"/>
                  <mkdir dir="${deploy.home}"/>

                  <mkdir dir="${doc.home}"/>
                  <mkdir dir="${doc.home}/api"/>

                  <mkdir dir="${build.home}"/>
                  <mkdir dir="${build.home}/WEB-INF" />
                  <mkdir dir="${build.home}/WEB-INF/classes"/>
                  <mkdir dir="${build.home}/WEB-INF/lib" />


                  <!-- ==================== "compile" Target ================================ -->

                  This only compiles java files that are newer
                       than their corresponding .class files.

                  <target name="compile" depends="prepare" >
                  <javac srcdir="${src.home}" destdir="${object.home}" debug="yes" >
                  <classpath refid="compile.classpath"/>

                  Copy the .class files to source tree. The Java2WSDL utility
                       needs these to generate the WSDL and Value Object Java files.

                  <copy todir="${src.home}">
                  <fileset dir="${object.home}"/>


                  <!-- ==================== "build" Target ================================== -->

                  This target builds the war file for the application
                       by first building the directory structure of the
                  application in ${build.home} and then creating the
                       war file using the ant <war> task

                  <target name="build" depends="compile" >

                  <!-- Copy all the webapp content (jsp's, html, tld's, xml, etc. -->
                  <!-- Note that this also copies the META-INF directory -->
                  <copy todir="${build.home}">
                  <fileset dir="${web.home}"/>

                  <!-- Now, copy all the Java class files -->
                  <copy todir="${build.home}/WEB-INF/classes">
                  <fileset dir="${object.home}"/>

                  <!-- Now, copy all the properties files, etc that go on the classpath -->
                  <copy todir="${build.home}/WEB-INF/classes">
                  <fileset dir="${src.home}">
                  <include name="**/*.properties" />
                  <include name="**/*.prop" />

                  <!-- Now, copy all the jar files we need -->
                  <copy todir="${build.home}/WEB-INF/lib">
                  <fileset dir="${lib.home}" />

                  <!-- Create the <war> file -->
                  <jar jarfile="${deploy.home}/${app.name}.war"


                  <!-- ==================== "deploy" Target ================================= -->

                  This target simply copies the war file from the deploy
                       directory into the Tomcat webapp directory.
                       This version of the file makes sure our files are
                       deployed into the Axis server webapp as well.

                       Note that deploying our files into the Axis webapp isn't
                       enoigh. In addition, there is a 'deploy' step required
                       to register the web service with Axis. Details are
                       provided in the chapter text.

                  <target name="deploy" depends="build" >

                  <!-- Copy the contents of the build directory -->
                  <copy todir="${webapps.home}" file="${deploy.home}/${app.name}.war" />


                  <!-- ==================== "doc" Target ==================================== -->

                  This task creates javadoc. It is dependent upon only the
                       'compile' target so it is not executed in a normal build.
                  As a result, the target needs to be run on its own.

                  <target name="javadoc" depends="compile">
                  <javadoc sourcepath = "${src.home}"
                  destdir = "${doc.home}/api"
                  packagenames = "${javadoc.pkg.top}.*"/>

                  • 6. Re: The import javax.servlet cannot be resolved - What do I wrong?
                    Hey Anjuli,
                    It's Amit from Delhi(India), I am working as a freelance developer using servlet and jsp. Regarding to your problem I would be happy to suggests you that you might need to set path and classpath for j2sdk. I am giving the path details for windows.
                    Right click on My Computer>Properties>Advance>Enviroment Variables:
                    On the top you will get User Variable parts>Click New Variable, named this as CLASSPATH from user variable and write following things:::%classpath%;c:\j2sdk1.4.2_08\lib;;c:\sun\appserver\lib\j2ee.jar;; in value part.
                    Save this.
                    On the bottom side you will get System Variable>Click on New Variable>Named this as JAVA_HOME, Write in value c:\j2sdk1.4.2_08
                    Save this
                    Create another new variable>Named this as J2EE_HOME>Write c:\sun\appserver;
                    Save this.
                    Select path from system variables>Click on Edit>Write c:\j2sdk1.4.2_08\bin;;c:\sun\appserver\bin;;
                    Save this.
                    Click on APPLY>Click on OK.
                    Close the window
                    Close the COMMAND PROMPT>>
                    Open another command prompt>>Compile the program written in java language.
                    Write javac filename with .java extension.>>Press enter>
                    You might get your solution by following these steps, Still having problem Call me on my mobile-"08010612406".
                    I would be happy to solve your problem..... Thanking you.
                    • 7. Re: The import javax.servlet cannot be resolved - What do I wrong?
                      Locking zombie thread, blocking self-advertising post.