I am a 50-year old chartered accountant doing J2EE Web Programming course in India. I am, of course, a novice in servlet programming. I attempted to compile a TestServlet.java. I am using bash terminal in my MacBookAir. It compiled when I used the command, "javac -classpath /Library/Tomcat/lib/servlet-api.jar TestServlet.java".
1) "/Library/Tomcat" folder in my MacBookAir refers, through a link set up through bash terminal at the time of installing Tomcat,
2) My .bash_profile in my "Users/sankar" folder in my MacBookAir, already,-
a) has set $CATALINA_HOME through its export statement to the above "/usr/local/apache-tomcat-7.0.39" and
b) has its CLASSPATH including "/$CATALINA_HOME/lib/servlet-api.jar".
3) My "/CATALINA_HOME/lib" folder in my MacBookAir, also actually includes the servlet-api.jar file.
The related screenshot files evidencing the above settings, I have attached to this query.
Why then does my javac command NOT compile my TestServlet.java without the -classpath attribute added to it ?
What should I do now, to just compile TestServlet.java using simple javac command ?
Kindly resolve my query, if time permits, soon.
Thanks in advance for your upcoming support
It sounds like there may be 2 distinct copies of Tomcat on your machine, unless there are some filesystem softlinks at play here.
However, you could have 100 copies and they wouldn't make any difference to the servlet compilation process, since the javac java compiler doesn't look at, know about, or even care about your Tomcat environment. Which is all to the good as far as people who use something like WebLogic instead of Tomcat.
The java compiler's sole assumption about compile-time class locations is that it knows about the classes defined within the JDK's directories. And then only to the extent that they have been officially registered as "safe"; you should not be arbitrarily dumping your own classes in there.
For everything else, an explicit set of classpath locations must be given. The javac compiler assumes nothing. Not even looking relative to the current directory. And, since the J2EE api definition classes are not part of the JDK internal classpath set, you therefore have to provide your own. Of which the api jars that come with Tomcat are among the most convenient.
Incidentally, building J2EE webapps using "brute force" compiles and batch files is rather tedious. For professional projects, most people use Ant or Maven to compile and assemble their webapps, and they are designed to make construction of Java components easier.
An IDE is no substitute for an Intelligent Developer.