How to compile servlets from command prompt?
Solution 1
classpath not path ... and you don't need it as an evironment variable. You can set the classpath for javac with option -cp or -classpath (several other ways are also available). javac uses the environment variable CLASSPATH to look for classes, that can be useful and can also be a source for hard-to-track-down-problems.
An example to compile a java file that uses a library(that is classes from outside the standard JavaSE) would be:
javac -classpath C:\apache-tomcat-7.0.23\lib\servlet-api.jar MyTestServlet.java
if your environmental variable CLASSPATH contains libraries you need you might want to do:
javac -classpath %CLASSPATH%;C:\apache-tomcat-7.0.23\lib\servlet-api.jar MyTestServlet.java
(please be aware that I don't have access to a windows machine, and therefore haven't tested the idiosyncratic parts of the syntax above) (also note that in this example "C:\apache-tomcat-7.0.23\lib\servlet-api.jar" is a jar file and not a directory which it might be from your question on your machine) For command line compiling on windows OS it is a good idea to have the environmental variable JAVA_HOME set correctly and the bin directory of the JDK in the PATH.
I do however suggest getting to write-compile-execute-deploy via/in an IDE for servlet development before figuring out how to do it with just the JDK from a command line. Java Servlets are not stand-alone executable classes but needs to be deployed into for example tomcat to be tested/used.
Solution 2
First copy the servlet-api.jar file from following path
C:\apache-tomcat-7.0.23\lib\servlet-api.jar;
and paste it in the bin folder of java software by following the path
C:\java\jdk1.6\bin;
Hope now you can successfully compile your servlet program.
Solution 3
1.You can copy your javax.servlet.jar in the jdk1.6\lib folder. 2.You can go to Control Panel\System\Advanced System Properties\Environment Variables
Enter the location of the jar in the CLASSPATH variable as follows:
Then compile and run the servlet.
Comments
-
Sanyifejű almost 2 years
I'd like to compile a very basic
servlet
from command prompt, but it is always unsuccessful and the compiler tells me the following:error: package javax.servlet does not exist.
I googled for the solution and I found that I need to include the
servlet.jar
libraries into my PATH. I believe I did. I strongly believe that the location for those libraries in my computer is:C:\apache-tomcat-7.0.23\lib\servlet-api.jar\
and the end (the relevant part) of my PATH is the following:
%JAVA_HOME%\bin;C:\apache-tomcat-7.0.23\lib\servlet-api.jar\
For me, it looks ok, but it is obviously not. Can anyone tell me what could be the problem?
-
Dave Newton about 12 years+1 for avoiding the CLASSPATH environment variable, which can lead to unexpected behavior. Better to include an example, though, for completeness.
-
esej about 12 yearsAgree an example, but I haven't seen a windows path in java in a decade or so. I'll try to update.
-
Dave Newton about 12 yearsBut it'd be an example of a classpath; the only difference is the separator,
;
in Windows,:
in real OSes. And quoting around paths with spaces, although the `` might work too--I don't really remember Windows stuff either--which is awesome :D -
Sanyifejű about 12 yearsjavac -classpath C:\apache-tomcat-7.0.23\lib\servlet-api.jar MyServlet.java You don't need the "=" sign after -classpath. But it was a great answer, that saved my day. Thanks
-
esej about 12 yearsThanks (was a bluder, I blame that I usually send command line classpath alterations via -D)
-
esej about 12 yearsHey, pr123 would you please mark this answer as the accepted answer then. This is the way stackoverflow is supposed to work :p . At the end of this faq entry is instructions how to do that. stackoverflow.com/faq#howtoask