File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Servlet Compilation 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 » Beginning Java
Bookmark "Servlet Compilation" Watch "Servlet Compilation" New topic
Author

Servlet Compilation

Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
Hello,
I am very new to this servlet. I am unable to execute my very first program through command prompt .
I have made all the directory structure required for the compilation process ( .java & web.xml).

In book code is written like this :
%javac -classpath/your path/tomcat/common/lib/servlet-api.jar -d classes src/Ch1Servlet.java

My servlet-api.jar file path is:
C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib

Kindly tell me how to get the right command :-(



Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
Tarun Oohri wrote:Hello,
I am very new to this servlet. I am unable to execute my very first program through command prompt .

If things do not fall as it should be, one tends to get irritated..so am i :-(
I have done environment variable settings also...still command prompt is saying " it can not recognize as internal or external command "
Following are the changes i have made in the environment variable ::
User Variable :-

Java_Home -- C:\Program Files\Java\jdk1.6.0;C:\Program Files\Java\jdk1.6.0\bin;

Path -- C:\Program Files\Java\jdk1.6.0\bin;%path%;

Tomcat_Home -- C:\Program Files\Apache Software Foundation\Tomcat 6.0;

System variables :-

classpath -- C:\Mywork;

path -- C:\Program Files\Java\jdk1.6.0\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\ATI Technologies\ATI Control Panel

Please let me know where i am wrong so as to fix this problem.

Thanks in Advance!!!
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

" it can not recognize as internal or external command "

Which command can it not recognise?
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
James Boswell wrote:" it can not recognize as internal or external command "

Which command can it not recognise?


%bin/startup

%javac -classpath/your path/tomcat/common/lib/servlet-api.jar -d classes src/Ch1Servlet.java

Help!!Help!!Help!!
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

Saying things like help!help! isn't going to make people help you more.

Can you tell us exactly what you have typed at the command line and the exact error message returned by the compiler?
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
James Boswell wrote:Saying things like help!help! isn't going to make people help you more.

Can you tell us exactly what you have typed at the command line and the exact error message returned by the compiler?


:-D i wish it could do so... Anyways, following are the codes which i tried to run :-

C:\Mywork>javac -classpath c:\Program Files\Apache Software Foundation\Tomcat 6.
0\lib\servlet-api.jar -d classes scr\Ch1Servlet.java
javac: invalid flag: Files\Apache
Usage: javac <options> <source files>
use -help for a list of possible options

C:\Program Files\Apache Software Foundation\Tomcat 6.0>bin\startup.sh
'bin' is not recognized as an internal or external command,
operable program or batch file.

Thanks !!!
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

What exactly is the problem that you are facing Tarun? Is it any/some or all of the following:

a) The tomcat container is not starting up and/or throwing errors.

b) Container starts but it fails to deploy the servlet.

c) Container starts and servlet is also deployed, but when you hit the servlet from the browser, you get error. OR

d) Your servlet java source itself is not compiling. There is no class being formed in WEB-INF/classes folder?

Please be discrete with precise information.


~ Mansukh
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

Greetings,
C:\Program Files\Apache Software Foundation\Tomcat 6.0>bin\startup.sh
'bin' is not recognized as an internal or external command,
operable program or batch file.


You are working in a Windows environment , so the bash shell is not available to you to directly execute bin files. To start the server use the startup.bat file. This is a window batch file . This should solve the problem of starting the server.

As for the other issue, try to enclose the paths in double qoutes.


Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
Mansukhdeep Thind wrote:What exactly is the problem that you are facing Tarun? Is it any/some or all of the following:

a) The tomcat container is not starting up and/or throwing errors.

b) Container starts but it fails to deploy the servlet.

c) Container starts and servlet is also deployed, but when you hit the servlet from the browser, you get error. OR

d) Your servlet java source itself is not compiling. There is no class being formed in WEB-INF/classes folder?

Please be discrete with precise information.

Hi Mansukdeep
Well, i guess my Tomcat is not starting up . I have tried starting it manually aswell as automatically but it fail to start...i also tried to open it at port 8080 but still no result..
Also, my servlet java source file is not compiling .

Thanks for your help.
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
Saif Asif wrote:Greetings,
C:\Program Files\Apache Software Foundation\Tomcat 6.0>bin\startup.sh
'bin' is not recognized as an internal or external command,
operable program or batch file.


You are working in a Windows environment , so the bash shell is not available to you to directly execute bin files. To start the server use the startup.bat file. This is a window batch file . This should solve the problem of starting the server.

As for the other issue, try to enclose the paths in double qoutes.

Hi Saif
Yes , i am using windows environment . I have tried using you command but still same error coming .

C:\Program Files\Apache Software Foundation\Tomcat 6.0\bin>startup.bat
'startup.bat' is not recognized as an internal or external command,
operable program or batch file.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Tarun Oohri wrote:
Hi Saif
Yes , i am using windows environment . I have tried using you command but still same error coming .

C:\Program Files\Apache Software Foundation\Tomcat 6.0\bin>startup.bat
'startup.bat' is not recognized as an internal or external command,
operable program or batch file.


OK Lets go step by step. Go to the above location in the bin directory. Then double click on the startup.bat file. Then tell me what happens.
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
Mansukhdeep Thind wrote:
Tarun Oohri wrote:
Hi Saif
Yes , i am using windows environment . I have tried using you command but still same error coming .

C:\Program Files\Apache Software Foundation\Tomcat 6.0\bin>startup.bat
'startup.bat' is not recognized as an internal or external command,
operable program or batch file.


OK Lets go step by step. Go to the above location in the bin directory. Then double click on the startup.bat file. Then tell me what happens.


Mansukhdeep , i am very sorry to say my bin folder does not contain startup file.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Mansukhdeep , i am very sorry to say my bin folder does not contain startup file.


No no. No need for sorry buddy. Lets rectify the problem at hand. OK? Download tomcat 6.0 from the net and extract it into the C:\ drive. Then make the necessary changes to startup.bat. Try starting it. If you have any issue, come back. Always happy to assist.

If things do not fall as it should be, one tends to get irritated..so am i :-(


There will be times when you will go through these mood swings caused by the silliest of human errors. Nobody matures instantly. My suggestion to you - Shut down your PC for now. Have a good night sleep. We will get back to this tomorrow. You will also be fresh then. Agree?
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
Mansukhdeep Thind wrote:
Mansukhdeep , i am very sorry to say my bin folder does not contain startup file.


No no. No need for sorry buddy. Lets rectify the problem at hand. OK? Download tomcat 6.0 from the net and extract it into the C:\ drive. Then make the necessary changes to startup.bat. Try starting it. If you have any issue, come back. Always happy to assist.

If things do not fall as it should be, one tends to get irritated..so am i :-(


There will be times when you will go through these mood swings caused by the silliest of human errors. Nobody matures instantly. My suggestion to you - Shut down your PC for now. Have a good night sleep. We will get back to this tomorrow. You will also be fresh then. Agree?


Thanks Mansukh...i will try to re-install it . I wont get sleep so early. I had been trying to figure out this from last night . Let me try and in case i did not get it right you can guide me tomorrow ..
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
Tarun Oohri wrote:
Mansukhdeep Thind wrote:
Mansukhdeep , i am very sorry to say my bin folder does not contain startup file.


No no. No need for sorry buddy. Lets rectify the problem at hand. OK?
If things do not fall as it should be, one tends to get irritated..so am i :-(


There will be times when you will go through these mood swings caused by the silliest of human errors.


Thanks Mansukh...i will try to re-install it .

Hi, I have got through half of the way.

* Compiled .java file
* Copied the web.xml & .class file to WEB-INF & classes respectively.
* Started the tomcat
* localhost:8080 is opening

But cant open my servlet class from http://localhost:8080/ch1/Serv1. It is giving 404 error.
Tomcat is asking for some username and password, which i tried to set up opening the( tomcat-users.xml )file in conf as follows ::

<tomcat-users>
<role rolename="manager-gui"/>
<user username="tomcat" password="s3cret" roles="manager-gui"/>
<!--
NOTE: By default, no user is included in the "manager-gui" role required
to operate the "/manager/html" web application. If you wish to use this app,
you must define such a user - the username and password are arbitrary.
-->
<!--
NOTE: The sample user and role entries below are wrapped in a comment
and thus are ignored when reading this file. Do not forget to remove
<!.. ..> that surrounds them.
-->
<
<role rolename="tomcat"/>
<role rolename="role1"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
<user username="both" password="tomcat" roles="tomcat,role1"/>
<user username="role1" password="tomcat" roles="role1"/>
>
</tomcat-users>
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Are you sure that tomcat is asking for the username and password or are you getting something like this:





[Thumbnail for Error.png]

Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
Mansukhdeep Thind wrote:Are you sure that tomcat is asking for the username and password or are you getting something like this:




No, its not showing like this. It ask for user name and password when i click on the links given on the http://localhost:8080 page.
When i try to open http://localhost:8080/ch1/Serv1 , it shows 404 error.

Now, when i tried to open the tomcat again..even the localhost:8080 page is not opening




[Thumbnail for New Picturnne.jpg]

Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
Tarun Oohri wrote:
Mansukhdeep Thind wrote:Are you sure that tomcat is asking for the username and password or are you getting something like this:




No, its not showing like this. It ask for user name and password when i click on the links given on the http://localhost:8080 page.
When i try to open http://localhost:8080/ch1/Serv1 , it shows 404 error.

Now, when i tried to open the tomcat again..even the localhost:8080 page is not opening



Some Fatal error is coming on starting tomcat


[Thumbnail for New Picture.jpg]

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Tomcat is asking for some username and password, which i tried to set up opening the( tomcat-users.xml )file in conf as follows ::


Revert whatever changes you made to the tomcat-users.xml file. Then start tomcat again.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

In the tomcat-users.xml file, you have:


You need to remove the first < character and the last > character.
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
James Boswell wrote:In the tomcat-users.xml file, you have:


You need to remove the first < character and the last > character.


Thanks Boswell and Mansukhdeep..I have got the entry to Tomcat with username and password but my servlet is not executing. Followings are the screen shots::


[Thumbnail for New icture (1).jpg]

[Thumbnail for New Picture (1).jpg]

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

You are seeing this message because the tomcat server is unable to locate your servlet. Have you made the entry of the servlet in the web.xml file? And if you have made it, are you sure it is correct with respect to the context path. Web application is very specific when it comes to locating resources placed in the application folder. Check those entries and make sure that relative paths are correct.
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
Mansukhdeep Thind wrote:You are seeing this message because the tomcat server is unable to locate your servlet. Have you made the entry of the servlet in the web.xml file? And if you have made it, are you sure it is correct with respect to the context path. Web application is very specific when it comes to locating resources placed in the application folder. Check those entries and make sure that relative paths are correct.


Mansukh i have throughly checked all the tomcat directory architecture and the mapping in the web.xml file, still the problem exists.
Hey, during tomcat installation do i need to download the " tomcat-apache-deployer " aswell ?? If yes then where to extract these files ? Maybe because of the absence of it , the servlet class is not executing ...whats say ?
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

I need the following things to go ahead with this:

a) Your web application structure. What is the context root name?

b) The servlet java source file

c) web.xml file



Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
Mansukhdeep Thind wrote:I need the following things to go ahead with this:

a) Your web application structure. What is the context root name?

b) The servlet java source file

c) web.xml file





Web application structure is :

tomcat ---webapps---ch1---WEB-INF --- (web.xml & classes)---Ch1Servlet.class
Ch1Servlet.class is in the classes folder

JAVA SOURCE CODE :


WEB.XML CODE :-
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Tarun Oohri wrote:
WEB.XML CODE :-


Two things:

a) Whenever dealing with xml tag values, make sure you give no spaces at all. Rename your servlet-name to same as Ch1Servlet.

b) Secondly, see how you have given the servlet-class value. You have written simply Ch1Servlet. How will tomcat find it? Your context application is ch1. Is that right? Whatever you map here in web.xml has to be relative to that folder name i.e ch1.Ch1Servlet instead of just Ch1Servlet.

Try and let me know if now you are able to hit the servlet without getting 404 error.
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
Mansukhdeep Thind wrote:
Tarun Oohri wrote:
WEB.XML CODE :-


Hi Mansukh,
No, its not like that. According to Head First Servlet & JSP, we should not prepend the context root with the servlet class name. We should prepend only that directory structure which is derived in classes folder when you compile your java classes (using -d) .
I think the problem is with the declaration code in the web.xml because in console somewhere it was given EncodingException . Then i tried remove this line of code [code=java]<?xml version="1.0" encoding="ISO-8851-1"?> , then in the localhost:8080 the RUNNING status turned TRUE from previously false. But still the servlet not executing (404 error).
Following are the screen shorts of before deleting the code line and after deleting the code line.



[Thumbnail for Before deleting.jpg]

[Thumbnail for After deleting.jpg]

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Which edition of the book are you referring to? What is the page no. on which the assignment is given? I will have to go through the problem to solve it.
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
Mansukhdeep Thind wrote:Which edition of the book are you referring to? What is the page no. on which the assignment is given? I will have to go through the problem to solve it.


2nd Edition ,July 2012
Check the diagrams on page number 72 &73. Then check page number 76 , servlet class path given is (com.example.web.BeerSelect) . If the context root should be prepend then it should be like (Beer-v1.WEB-INF.classes.com.example.web.BeerSelect)
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Please share tomcat start up console log. Also, check if your server.xml file has entry for ch1 application. Share your server.xml file. I will have a look at it.
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
Mansukhdeep Thind wrote:Please share tomcat start up console log. Also, check if your server.xml file has entry for ch1 application. Share your server.xml file. I will have a look at it.


I could not find ch1 in server.xml...Have a look...Thanks..

SERVER.XML



[Thumbnail for New Picture (2).jpg]

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Try connecting via port number 8443 instead of 8080. Does it allow?
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
Mansukhdeep Thind wrote:Try connecting via port number 8443 instead of 8080. Does it allow?


No it do not :-( ....i guess i have to switch to IDE....Can anybody rectify this problem ??
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Tarun Oohri wrote:
Mansukhdeep Thind wrote:Try connecting via port number 8443 instead of 8080. Does it allow?


No it do not :-( ....i guess i have to switch to IDE....Can anybody rectify this problem ??


Nobody but only you can only rectify your problem. Switching to IDE would but make thinks more difficult as learning how IDE works is itself a task. Having said that, I would not want to just give up on this. I tried the same on my machine. It is working. I did it on Eclipse by creating a dynamic-web-project. I am successfully able to hit the servlet and display the date and time. Tell me one thing- How does your tomcat know that application named ch1 is present?
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
Mansukhdeep Thind wrote:
Tarun Oohri wrote:
Mansukhdeep Thind wrote:Try connecting via port number 8443 instead of 8080. Does it allow?


No it do not :-( ....i guess i have to switch to IDE....Can anybody rectify this problem ??


Nobody but only you can only rectify your problem. Switching to IDE would but make thinks more difficult as learning how IDE works is itself a task. Having said that, I would not want to just give up on this. I tried the same on my machine. It is working. I did it on Eclipse by creating a dynamic-web-project. I am successfully able to hit the servlet and display the date and time. Tell me one thing- How does your tomcat know that application named ch1 is present?


Hmmmmm...By searching for it in webapps folder ?
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

I tried the assignment without using IDE like the way you are trying. I am successfully able to hit the servlet. There is some very basic error you are making. Check carefully.
Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

Your logs in New Picuter(2) does not show the deployement of the ch1 project. A log entry should come up when tomcat finds a directory in your webapps.

Do one thing, re deploy your web application and then stop and then start the server and post the startup logs here.
Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 172
Saif Asif wrote:Do one thing, re deploy your web application and then stop and then start the server and post the startup logs here.


Good News !!! Finally successfully executed ... I just made the whole new application systematically and copied the header of xml from MyEclipse n got it
Thanks guys for your support ... Mansukh thanks for pushing me to solve the problem rather than running from it


[Thumbnail for New Picture (1).jpg]

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Job well done. You are always welcome Tarun. The important thing is learn not to give up. You might not get there in the first try, or maybe the second or third. The main thing self-evaluation andl keep trying. As for assistance, we are always here if you get stuck.
 
Don't get me started about those stupid light bulbs.
 
subject: Servlet Compilation
 
Similar Threads
basic servlet compilation difficulty
problem with class path
struts class file error
Compiling a Servlet
Compile Servlet