aspose file tools*
The moose likes Beginning Java and the fly likes Facing problem in compiling the first servlet program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Facing problem in compiling the first servlet program" Watch "Facing problem in compiling the first servlet program" New topic
Author

Facing problem in compiling the first servlet program

geet kaur
Ranch Hand

Joined: Sep 03, 2008
Posts: 78
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...
Charles 'King
Ranch Hand

Joined: Jul 05, 2009
Posts: 56

Take a look at this: http://download.oracle.com/javase/1.5.0/docs/tooldocs/windows/classpath.html and http://download.oracle.com/javase/1.5.0/docs/tooldocs/windows/javac.html
geet kaur
Ranch Hand

Joined: Sep 03, 2008
Posts: 78
Hey Charles

Thanks for that link,i have set the classpath according to that but still its giving the same error
mahesh shastri
Greenhorn

Joined: Sep 04, 2010
Posts: 11
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

Joined: Feb 03, 2004
Posts: 576

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


Cheers,
Naren
(OCEEJBD6, SCWCD5, SCDJWS, SCJP1.4 and Oracle SQL 1Z0-051)
geet kaur
Ranch Hand

Joined: Sep 03, 2008
Posts: 78
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

Joined: Jan 10, 2002
Posts: 60082
    
  65

As this is more about learning to compile than it is about Servlets, it's been moved to a more appropriate location.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
geet kaur
Ranch Hand

Joined: Sep 03, 2008
Posts: 78
Hey Naren

Just wanted to be familiar with this approach first,and then would think of switching to the IDE
mahesh shastri
Greenhorn

Joined: Sep 04, 2010
Posts: 11
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

Joined: Oct 13, 2005
Posts: 36599
    
  16
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

Joined: Sep 03, 2008
Posts: 78
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

Joined: Sep 03, 2008
Posts: 78
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

Joined: Aug 16, 2005
Posts: 13884
    
  10

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


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
geet kaur
Ranch Hand

Joined: Sep 03, 2008
Posts: 78
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

Joined: Sep 04, 2010
Posts: 11
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

Joined: Oct 13, 2005
Posts: 36599
    
  16
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

Joined: Nov 01, 2010
Posts: 3
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


OCPJP 6.0 (98%)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Facing problem in compiling the first servlet program
 
Similar Threads
Still cant complile servlet after setting classpath
package javax.servlet does not exist
Still cant complile servlet after setting classpath
Head First and I Can't make a .class file?
Servlet basic program