Java – Compiling a servlet in Windows XP

javaservlets

How do I compile and run a servlet from the Command Prompt?

I got an error when I tried to compile using Apache Tomcat 6.0.16.

What is the exact command I should be using?

Best Answer

How do I compile and run a servlet from the Command Prompt?

For the first part of the question, you'll need to put the servlet API on you classpath:

javac -cp $TOMCAT_HOME/lib/servlet-api.jar *.java

For the second part, what you are trying to achieve is a bit unclear. A Servlet is intended to be packaged in a WAR and deployed in a Servlet container (like Tomcat). A Servlet doesn't have a main() method, it's not intended to be run on the command line.

Actually, you should start with a good tutorial like Introduction to Developing Web Applications and get some IDE support. NetBeans is not my favorite IDE but they have very good educational material and, in your case, I think it would be a good starting point.