wood burning stoves 2.0*
The moose likes Servlets and the fly likes How to compile a servlet without adding -classpath attribute to javac command Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to compile a servlet without adding -classpath attribute to javac command" Watch "How to compile a servlet without adding -classpath attribute to javac command" New topic

How to compile a servlet without adding -classpath attribute to javac command

Ra Kumar Sankar

Joined: Jun 17, 2013
Posts: 1
Dear All

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,
to "/usr/local/apache-tomcat-7.0.39".
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

Ra K Sankar
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15962

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.

Customer surveys are for companies who didn't pay proper attention to begin with.
subject: How to compile a servlet without adding -classpath attribute to javac command
Similar Threads
java.lang.NoClassDefFoundError: org/apache/tomcat/util/buf/B2CConverter
Having Classpath trouble on my new iMac
Problem running Tomcat 5.5.9 on OS X Tiger
trouble importing javax.servlet
Learning Servlet code