I want to compile servlets outside of NetBeans. I made a simple Hello World servlet that produced these compiler errors.
import javax.servlet.ServletException;
^
ServletTester.java:4: package javax.servlet.http does not exist
import javax.servlet.http.*;
^
ServletTester.java:6: cannot find symbol
symbol: class HttpServlet
public class ServletTester extends HttpServlet {
^
ServletTester.java:7: cannot find symbol
symbol : class HttpServletRequest 开发者_C百科
location: class ServletTester
protected void processRequest(HttpServletRequest request, HttpServletResponse response) t
^
ServletTester.java:7: cannot find symbol
symbol : class HttpServletResponse
location: class ServletTester
protected void processRequest(HttpServletRequest request, HttpServletResponse response) t
^
ServletTester.java:7: cannot find symbol
symbol : class ServletException
location: class ServletTester
protected void processRequest(HttpServletRequest request, HttpServletResponse response) t
6 errors
Clearly, the javax.servlet
package cannot be located. I have javax.servlet.jar
from a GlassFish install, but if I do javac ServletTester.java -classpath /opt/glassfish3/glassfish/modules/
I still get the same errors.
What is the proper way to manually compile servlets?
Try this:
$ javac -classpath .:/opt/glassfish3/glassfish/modules/javax.servlet.jar ServletTester.java
Please note that JAR filenames in classpath must be fully specified. Just their containing directory is not enough. Using wildcards is also allowed (as appointed by one comment).
Plus, consider maven. It has archetypes that give you working boilerplate for generating web applications with many, many different application frameworks; there's a simple web app archetype that would automate what you're trying to do here.
Manually compiling java usually isn't a good plan; build tools exist to automate the processing and lifecycle of projects. They're mature and useful.
精彩评论