File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlets" Watch "servlets" New topic


anjali ramachandran

Joined: Aug 12, 2009
Posts: 1
I am trying to run a servlet in command prompt.But I am unable to compile it into a
class file.I have set the classpath but still I am getting package javax.servlet.* not found error.
My Program is as folows:

import javax.servlet.*;

public class WelcomeServlet extends HttpServlet {

public void init(ServletConfig config) throws ServletException {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
* Get the value of form parameter
String name = request.getParameter("name");
String welcomeMessage = "Welcome "+name;
* Set the content type(MIME Type) of the response.

PrintWriter out = response.getWriter();
* Write the HTML to the response
out.println("<title> A very simple servlet example</title>");
//out.println("<a href="/servletexample/pages/form.html">"+"Click here to go back to input page "+"</a>");


public void destroy() {

classpath I have set are
TOMCAT C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar
JAVA_HOME C:\Program Files\Java\jdk1.6.0\bin

Can you please find me some alternative

Thanking you in advance
Gerardo Tasistro
Ranch Hand

Joined: Feb 08, 2005
Posts: 362
An alternative I'd suggest is running it in a server like Tomcat. Of all the things that come to mind as possible limitations to running a servlet from a command line, lacking a main is top on my list.
Swastik Dey

Joined: Jan 08, 2009
Posts: 1584


First of all you can't execute a servlet from command prompt. It needs container for the execution. For e.g. tomcat, jboss, webspher, weblogic etc. Secondly the problem what you are facing is a classpath issue. Your classpath is not properly pointing to servlet-api.jar. Third problem noticeable in your code is HttpServlet belongs to javax.servlet.http package, however you haven't imported that.

Ankit Garg

Joined: Aug 03, 2008
Posts: 9465

anjali please Use A Meaningful Subject Line for your topics. Writing only Servlet in the title doesn't let us understand what the question is about.

Also when you post a source code, please Use Code Tags...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13036
When you say:
I am trying to run a servlet in command prompt

do you mean you are trying to compile a servlet? Thats what the package not found sounds like.

The package javax.servlet is NOT in the standard library, you need the packages distributed with a servlet container such as Tomcat in your classpath for the compiler to see them. Defining a TOMCAT variable is not enough.

Shruthi Sam
Ranch Hand

Joined: Jul 09, 2008
Posts: 73
Make sure that the servlet-api.jar is in your classpath.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63852

Please be sure to take the time to compose descriptive subjects for your posts; read this for more information.

Using a title of "servlets" in a forum completely dedicated to questions on Servlets isn't very helpful. What if everyone title theri servlet questions "servlets"?

Please go back and change your post to add a more meaningful subject by clicking the

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: servlets
It's not a secret anymore!