Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 I am using bash terminal in my MacBookAir. It compiled when I used the command, "javac -classpath /Library/Tomcat/lib/servlet-api.jar".

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 without the -classpath attribute added to it ?
What should I do now, to just compile using simple javac command ?

Kindly resolve my query, if time permits, soon.
Thanks in advance for your upcoming support

Ra K Sankar
Saloon Keeper
Posts: 19086
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!