0

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?

5
  • 1
    To get more specific help, please post the error you got. In the future, please do not ignore errors as if they are for decoration. They contain important information about the cause of the problem. You know, once the cause is understood, the solution is crystal clear. Commented Dec 1, 2009 at 12:13
  • yea danks much Mr. Balusc..in future will post ma errors too.. Commented Dec 2, 2009 at 5:38
  • Bt ma doubt is whether i hav to include path for jdk or tomcat..or else both.. Commented Dec 2, 2009 at 5:43
  • Try to write normal English sentences and/or run a spellchecker. This is not a speed chat/sms/IM place. I can hardly take your questions/comments for serious; they do not encourage me (and likely many others) on responding. You know perfectly that words like bt, ma, hav and so on makes no utter sense. Please work on that. It is not "cool" to do so or so. Commented Dec 2, 2009 at 20:07
  • Oh, I should have added this link: catb.org/esr/faqs/smart-questions.html. Good luck surviving at the internets as a real professional. Commented Dec 2, 2009 at 20:08

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

okei..danks Mr.Pascal..but actually ma doubt is whether i have to mention jdk path or tomcat path or else both..
0

What I would suggest, is reading some tutorials. Here are a few pointers:

  • servlets require a servlet-container (Tomcat, for example) in order to execute. They are server-side components whichi handle HTTP requests.
  • in order to compile a servlet you need the servlet-api in your classpath. Tomcat has this api in its libs, but Tomcat cannot itself compile servlets - javac does.

2 Comments

dANKS much Mr.Bozho..bt ma doubt is whether i hav to include path for jdk or tomcat..or else both..
you have to include the path to servlet-api.jar, and use javac to compile
0

If you just want to compile it for the sake of seeing if it is syntactically correct you can compile it like any other Java source file with javac. You just need to make sure that any dependency jars are included on the classpath. For standard servlets this is typically standard.jar and jstl.jar which is distributed with tomcat. So something like:

javac -classpath C:\deps\standard.jar;C:\deps\jstl.jar MyServlet.java

You can typically find the dependency jar files at:

  • tomcat/webapps/examples/WEB-INF/lib/jstl.jar
  • tomcat/webapps/examples/WEB-INF/lib/standard.jar

You can copy these to your project directory or just add them to your classpath directly.

If you're still having problems beyond this post the specific error output you're getting as BalusC suggested.

Comments

0

1) Normal command prompt programing

javac -classpath ".;D:\xampp\tomcat\lib\servlet-api.jar"

D is my driver path can you install to other driver

2) Another easy to use compiling system is Programmer's Notepad (open and free) This system can using for java compiling.

following these steps:

  1. Open PP
  2. Tool -> option -> select tool tab
  3. Select language java to upper tab
  4. Selecting add button
  5. Filling form
    • Name :- Servelt
    • Command:- E:\Program Files\java\jdk1.6.0_25\bin\javac.exe // JDK javac path
    • Folder :- C:\java // your using or file folder
    • Parameter :- %f -classpath ".;D:\xampp\tomcat\lib\servlet-api.jar" // this copy and paste and changing a driver Ex:- D
    • Shortcut:- F12 // selecting you lick button
    • Save:- current file // or you lick

After Ok

  1. view -> output or press F8

Ok finish Coding after press F12

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.