Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Facing problem in compiling the first servlet program

 
geet kaur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to compile this simple servlet program from head first java,but i m stuck

Following,whatever is there in the book,I set the classpath
like this javac -classpath C:\ApacheSoftwareFoundation\tomcat5.5\common\lib\servlet-api.jar -d classes src\Ch1Servlet.java
1.package javax.servlet.http.* does not exist
2.cannot find symbol HttpServlet.

Please Help...
 
geet kaur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Charles

Thanks for that link,i have set the classpath according to that but still its giving the same error
 
mahesh shastri
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Geet,
Try this .. right click your MyComputer icon on desktop. Goto -> Advanced Tab. There you will find Environmental variables
click on that you will find all the system variables lis over there.
In UserVariables section you can create environmental variables. Now click on new
1) variable name : classpath

value :<path till lib of insatalled java>e.g(C:\Program Files\Java\jdk1.5.0\lib);.;<path till lib of tomcat> e.g(C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib);<path till jsp-api.jar>;<path till servlet-api.jar>e.g(C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar;C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\jsp-api.jar);.;

after writing this all click ok. Your classpath environmental variable will be created which you can see it in the list.
2)variable name : path

value: <path till bin of installed java>;<path till bin of tomcat>
click ok to create this variable.

3)variable name :CATALINA_HOME
value :<path till tomcat base> e.g(C:\Program Files\Apache Software Foundation\Tomcat 6.0)
4)variable name : JAVA_HOME
value : <path till java base> e.g(C:\Program Files\Java\jdk1.5.0)

Now apply these variables and compile your program your program will be compiled
Regards
 
Naren Chivukula
Ranch Hand
Posts: 577
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure why you can't you IDE like Eclipse which is freely available and creating a simple servlet class doesn't take more than a minute through wizard. I'm not trying to force to use IDE but it looks like you are working on Servelts recently. So, why not start with an easier approach?

Cheers,
Naren

----------------------
SCJP
SCDJWS
SCWCD
----------------------
 
geet kaur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Mahesh

you were a great help,and i followed your instructions step by step
crossed my fingers,but still no use,its not compiling yet again...
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As this is more about learning to compile than it is about Servlets, it's been moved to a more appropriate location.
 
geet kaur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Naren

Just wanted to be familiar with this approach first,and then would think of switching to the IDE
 
mahesh shastri
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Geet ,
May I know what exactly you are doing ? If I guess you might be adding classpath clause while compiling.
Actually goto to your project dir then to WEB-INF . Now use javac -d classes src\<ClassName.java>. It will create
.class file in classes folder but classes folder should be there under WEB-INF\classes
Hope it will work now
Regards
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:. . . it's been moved to a more appropriate location.
I thought this thread looked unfamiliar, and was wondering why nobody has yet given the correct way to set a system classpath-not at all.

Advice that you ought to set a system-wide classpath is usually mistaken; you should set a classpath for each application with the -cp flag.
 
geet kaur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Mahesh

I am Compiling the program from the project directory first,once it gets compiled only then do i need to copy n paste it in the web-inf-classes folder,as told in the book
but as i see,going by the same i am not able to compile it.
 
geet kaur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey campbell
Thanks for your advice,but if you could ellaborate it,as it would be of great help...
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
geet kaur wrote:like this javac -classpath C:\ApacheSoftwareFoundation\tomcat5.5\common\lib\servlet-api.jar -d classes src\Ch1Servlet.java
1.package javax.servlet.http.* does not exist
2.cannot find symbol HttpServlet.

Are you sure that the directory "C:\ApacheSoftwareFoundation\tomcat5.5\common\lib" exists and contains a file named servlet-api.jar?

You're not just leaving out the spaces in the path, are you? Because "C:\ApacheSoftwareFoundation" is not the same as "C:\Apache Software Foundation". If the directory name contains spaces, enclose the name in quotes, for example:

javac -classpath "C:\Apache Software Foundation\tomcat5.5\common\lib\servlet-api.jar" -d classes src\Ch1Servlet.java
 
geet kaur
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Jesper
I am setting the classpath like
javac -classpath C:\ProgramFiles\ApacheSoftwareFoundation\Tomcat5.5\common\lib\servlet-api.jar
I am not putting in any spaces in the path.

But Again i am getting this same error
 
mahesh shastri
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Geet
As young said there must be gaps between folder name. I think it should be c:\Program Files\Apache Software Foundation.
Try to copy path from address bar when you access those folder and paste it while compiling on command promt.
Other thing javac -d classes will create .class file in classes folder. you dont need to copy. Let me know what exactly you are
doing.

Regards
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
geet kaur wrote:Hey campbell
Thanks for your advice,but if you could ellaborate it,as it would be of great help...
If you set a system classpath, all applications will use it. Even though they require different classpaths. So a system classpath will grow and grow. Set your classpath as Jesper has told you; if you create a jar, put the classpath in its manifest file.
 
rangi vikas
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi geet, you can try like this on command prompt its working for me.

C:\<tomcat root directory>\webapps\<your application folder>\WEB-INF>javac -cp "<tomcat root directory>\lib\servlet-api.jar";. -d classes src\Ch1Servlet.java

here "." in -cp represent system defined classpath. java compiler(javac) will first look HttpServlet class in servlet-api.jar and after that it looks in all jar files which are set in system defined classpath
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic