3

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                                                               
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?

Pieter
  • 31,619
  • 76
  • 167
  • 242

2 Answers2

6

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).

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
  • 3
    You should be able to use wildcards: [As a special convenience, a class path element containing a basename of * is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR.](http://download.oracle.com/javase/6/docs/technotes/tools/solaris/javac.html#options) – McDowell Apr 26 '11 at 14:04
2

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.

Joseph Ottinger
  • 4,911
  • 1
  • 22
  • 23
  • 3
    It is a good idea to manually compiling java if you want to fully understand what is going on. Otherwise, I agree with you. – Burkhard Apr 26 '11 at 14:03
  • Sure, I can fully agree with that - but as a goal, it's pretty much a one-shot deal, and understanding compilation classpaths (and runtime classpaths) is not a servlet-oriented thing. I'd say if you're trying to understand the classpath, you should use Java SE and a simple external library before bothering with servlets. – Joseph Ottinger Apr 26 '11 at 14:05
  • @Burkhard It is a good idea to manually compile java *once in your life* if you want to fully understand what is going on. But from then on you should use a proper toolset :-) – Sean Patrick Floyd Apr 26 '11 at 14:07
  • @The Maven recommendation: I'm already having a hard time wrapping my head around Tomcat/servlets (I'm used to the convenience of uploading PHP scripts to a server) so I don't have the mental bandwidth to learn yet another tool that requires specific knowledge about its usage and syntax, even if it may end up making things easier for me in the long run. – Pieter Apr 27 '11 at 09:44