Results 1 to 13 of 13
  1. #1
    lse123 is offline Senior Member
    Join Date
    Oct 2008
    Posts
    116
    Rep Power
    0

    Default I can not compile servlets although normal java can... errors shown

    I install JDK but when I went to compile Servlets I get error below, where is the wrong ? I compile/run success normal java...

    Java Code:
    C:\J-DB-ACCESS\Servlets-JSPs\Chapter9\WEB-INF\classes>javac Test21.java 
    
    C:\J-DB-ACCESS\Servlets-JSPs\Chapter9\WEB-INF\classes>java Test21 
    String test: str = hello 
    
    C:\J-DB-ACCESS\Servlets-JSPs\Chapter9\WEB-INF\classes>cd C:\J-DB-ACCESS\Servlets-JSPs\Chapter9 
    
    C:\J-DB-ACCESS\Servlets-JSPs\Chapter9>javac HelloPlantServlet.java 
    HelloPlantServlet.java:4: package javax.servlet does not exist 
    import javax.servlet.*; 
    ^ 
    HelloPlantServlet.java:5: package javax.servlet.http does not exist 
    import javax.servlet.http.*; 
    ^ 
    HelloPlantServlet.java:7: cannot find symbol 
    symbol: class HttpServlet 
    public class HelloPlantServlet extends HttpServlet   { 
                                           ^ 
    HelloPlantServlet.java:10: cannot find symbol 
    symbol  : class HttpServletRequest 
    location: class HelloPlantServlet 
       protected void doGet( HttpServletRequest request, //Executes in response to a "get" public 
                             ^ 
    HelloPlantServlet.java:11: cannot find symbol 
    symbol  : class HttpServletResponse 
    location: class HelloPlantServlet 
           HttpServletResponse response )               //type of request from a Web client. 
           ^ 
    HelloPlantServlet.java:12: cannot find symbol 
    symbol  : class ServletException 
    location: class HelloPlantServlet 
             throws ServletException, IOException 
                    ^ 
    6 errors 
    
    C:\J-DB-ACCESS\Servlets-JSPs\Chapter9>

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    the Servlet API is not part of the standard edition Java. What you can do here is get a "servlet.jar" from something that does have this, such as a tomcat distribution, and copy this into your workspace, or add it to the -classpath of the javac (or refer to the path to where it is in the installed tomcat instance).

    For example, in tomcat 6, this is the the lib/servlet-api.jar file.

  3. #3
    lse123 is offline Senior Member
    Join Date
    Oct 2008
    Posts
    116
    Rep Power
    0

    Default

    I find it in tomcat 6 well copy it to C:\Sun\AppServer\lib ? to compile servlets tomcat must be started ?

  4. #4
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    no, you should not need tomcat started for compiling servlets. The java compiler just creates a .class file from the .java source files, and this would make use the .jar files on its classpath during compilation.

    You would need to have tomcat running, and your servlet deployed in an application, deployed into a running tomcat instance to run and use the servlet.

  5. #5
    lse123 is offline Senior Member
    Join Date
    Oct 2008
    Posts
    116
    Rep Power
    0

    Default

    well how fix compilation errors about servlets ONLY ?

  6. #6
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    oh, I said this before.
    What you can do here is get a "servlet.jar" from something that does have this, such as a tomcat distribution, and copy this into your workspace, or add it to the -classpath of the javac (or refer to the path to where it is in the installed tomcat instance).

  7. #7
    lse123 is offline Senior Member
    Join Date
    Oct 2008
    Posts
    116
    Rep Power
    0

    Default

    I FOUND : servlet-api.jar in TOMCAT 6 (THIS ONLY NEEDED CORRECT?)
    C:\Program Files\Apache Software Foundation\Apache Tomcat 6.0.18\lib\servlet-api.jar

    WHERE insert it(FOLDER) to can compile SERVLETS ?
    some folders follow...?

    C:\Program Files\Java\jdk1.6.0_15\bin
    C:\Sun\AppServer\lib
    C:\Program Files\Java\jdk1.6.0_15\src\javax

    TO set classpath in System Vars what to insert ? currently is:
    .;C:\Program Files\Java\jre6\lib\ext\QTJava.zip

    another way is to copy servlet-api.jar to directory is servletsample.java (and this DIR USED TO COMPILE)?

  8. #8
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    Yes, that's the servlet-api.jar file you found in the
    C:\Program Files\Apache Software Foundation\Apache Tomcat 6.0.18\lib\servlet-api.jar
    Usually, instead of copying it into one of those Java 'system' folders, I create a 'lib' folder inside my project folder, and copy it into there, and then have the what ever does the java compile use that lib/servlet-api.jar file included into its CLASSPATH.

    For example, if my project was called MyProject, I would have
    Java Code:
     MyProject/
       |
       +- lib/
       +- src/
       +- build.xml
    Where the src/ folder contains my Java packages and Java classes.
    Where the lib/ folder contains any of these third party libraries or API that my code depends on (such as this servlet jar file here)
    And the build.xml is an Ant build file to do all the work for me to invoke javac, and what ever else is required to build the application for me, and in a general independent way of any IDE environment or operating system. In the case of a web application module, this might do the compile java sources, and also create a .war file to package them, along with any .jar files, the web.xml, and JSP files, and so on.

    The self-contained library dependencies inside the project module make sense, to me, because I can just zip up, or store this into a version control system, such as Subversion, to share my code with other people, and they only have to get the project folder to be able to use it, no complex steps to set up class paths, or copy jar files to some other place on their system.

    This also works well if using an IDE, such as eclipse, where the module can be set up as a "project", and allow the IDE to be configured with the .jar files in the lib folder as well.

    In a web-application project I would also have a WebContent folder, and inside that a WEB-INF/ folder. Generally I just try to follow the convention for folders with what Eclipse does for me when I would use it to create a new dynamic web application module, that way I can use my module in eclipse if I want to, but also able to compile it on a stand-alone command prompt.

  9. #9
    lse123 is offline Senior Member
    Join Date
    Oct 2008
    Posts
    116
    Rep Power
    0

    Default isn't easier to write classpath in System Vars rather like:

    TO set classpath in System Vars what to insert ? currently is:
    .;C:\Program Files\Java\jre6\lib\ext\QTJava.zip

    isn't easier to write classpath in System Vars rather like:
    .;C:\Program Files\Java\jre6\lib\ext\QTJava.zip;C:\Program Files\Apache Software Foundation\Apache Tomcat 6.0.18\lib\servlet-api.jar

    I am going now to try it ? is any error ?

  10. #10
    lse123 is offline Senior Member
    Join Date
    Oct 2008
    Posts
    116
    Rep Power
    0

    Default

    needed RESTART the PC ?

  11. #11
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    Not sure if you need to restart, should likely need to reopen the CMD window to have the environment variable take effect .

    System classpath probably will work too, I got out of the habbit of doing that because over time I end up working with different versions of the same product, and I forget that I installed a .jar file into the system classpath, which could end up overriding the (newer version) inside the application i am running, and that takes me a while to figure out why something isn't working. I had that problem more with JDBC drivers though.

  12. #12
    bdtagar is offline Member
    Join Date
    Jun 2009
    Posts
    11
    Rep Power
    0

    Default

    Hey,
    I think this will work:
    1. put this servlet-api.jar file in jdk-installation-dir/lib/ folder
    2. set classpath=c:\jdk-install-dir\lib\servlet-api.jar
    3. reopen console window, and issue javac

    that will compile servlets

    bye...

  13. #13
    lse123 is offline Senior Member
    Join Date
    Oct 2008
    Posts
    116
    Rep Power
    0

    Default finally this worked

    finally this worked: write classpath in System Vars rather like:
    .;C:\Program Files\Java\jre6\lib\ext\QTJava.zip;C:\Program Files\Apache Software Foundation\Apache Tomcat 6.0.18\lib\servlet-api.jar

Similar Threads

  1. cant compile servlets
    By warriorKnight in forum New To Java
    Replies: 1
    Last Post: 12-17-2009, 07:27 PM
  2. compile ignoring dependency errors
    By Geoffrey in forum Java Servlet
    Replies: 1
    Last Post: 09-10-2009, 07:45 PM
  3. Compile Servlets with Apache ANT
    By Lukas in forum Advanced Java
    Replies: 2
    Last Post: 08-06-2009, 12:39 PM
  4. eclipse 3.4 is shown errors
    By ramaksilvertide in forum New To Java
    Replies: 2
    Last Post: 02-10-2009, 12:51 AM
  5. Help with Compile time errors
    By bri1547 in forum New To Java
    Replies: 2
    Last Post: 08-25-2008, 12:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •