File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compiling a Servlet

 
Suvojyoty Saha
Greenhorn
Posts: 25
Firefox Browser Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I'm new to Servlets. I'm having a lot of difficulty in compiling and creating the class file of the servlet.

As per listed in Head First i've created the directory structure
C:\Documents and Settings\dodo\My Documents\MyProject\beerV1\src\com\example\web\BeerServlet.java
C:\Documents and Settings\dodo\My Documents\MyProject\beerV1\classes
C:\Documents and Settings\dodo\My Documents\MyProject\beerV1\etc\web.xml
C:\Documents and Settings\dodo\My Documents\MyProject\beerV1\lib\servlet-api.jar
C:\Documents and Settings\dodo\My Documents\MyProject\beerV1\web


my path is set as C:\Program Files\Java\jdk1.6.0_23\bin
classpath as D:\tomcat\common\lib

after going to the directory i'm entering the following command

C:\Documents and Settings\dodo\My Documents\MyProject\beerV1>
javac -classpath D:\tomcat\common\lib\servlet-api.jar;classes:. -d classes src/com/example/web/BeerSelect.java


but getting this error

javac: file not found: src\com\example\web\BeerSelect.java
Usage: javac <options> <source files>
use -help for a list of possible options


could someone please help me resolve this issue.
 
Mohamed Sanaulla
Saloon Keeper
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are using a wrong set of file seperators- "/" or "\". Also note that you would have to use the servlets in a web container- example Tomcat so that it will manage the servlets lifecylce.
 
Suvojyoty Saha
Greenhorn
Posts: 25
Firefox Browser Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried using both the file seperators "/" and "\" but still getting the same msg. File not found.

I also tried to compiling in the following manner :

C:\Documents and Settings\dodo\My Documents\MyProject\beerV1\src\com\example\web>
javac -classpath D:\tomcat\common\lib\servlet-api.jar BeerSelect.java



but still getting this error

javac: file not found: BeerSelect.java
Usage: javac <options> <source files>
use -help for a list of possible options

 
Ankit Tripathi
Ranch Hand
Posts: 199
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Suvojyoti,welcome to javaranch.........

most possible reason can be extension problem.if you are sure that file java file exists in directory from where you are compiling then use "dir" command to list the contents of that directory,check whether it is "BeerSelect.java" or "BeerSelect.java.txt".
also check for spaces and other punctuation characters in name of java file.
 
Vijitha Kumara
Bartender
Posts: 3908
9
Chrome Fedora Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try wrapping the path to jar inside quotes.

javac -classpath "D:\tomcat\common\lib\servlet-api.jar" BeerSelect.java
 
Suvojyoty Saha
Greenhorn
Posts: 25
Firefox Browser Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit for the warm welcome

here's screen shot


i've checked for the file extension it seems correct. Is there any other options that i can try?
 
Suvojyoty Saha
Greenhorn
Posts: 25
Firefox Browser Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijitha the same issue still persists.

for servlet compilation we only need the servlet-api.jar i guess

For the following command "javac -classpath "D:\tomcat\common\lib\servlet-api.jar" BeerSelect.java"

-classpath "D:\tomcat\common\lib\servlet-api.jar" ---- denoting the path where the servlet-api resides
and
BeerSelect.java in the current path "C:\Documents and Settings\dodo\My Documents\MyProject\beerV1\src\com\example\web"

please do correct me if i'm wrong.
 
Deepakkumar Devarajan
Ranch Hand
Posts: 54
Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the class path, you have colon instead of semi colon. Also enclose the path in double quotes.
 
Deepakkumar Devarajan
Ranch Hand
Posts: 54
Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OMG , none of us noticed it. You created BeerServlet.java and tryibg to execute BeerSelect.java as it is in HeadFirst book. lol , ,
 
Suvojyoty Saha
Greenhorn
Posts: 25
Firefox Browser Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a Lot Deepak... you are a life saver..

@Ankit: My bad i still wasn't able to figure out problem. Thanks a lot for your help.

@Other: Thanks a lot for your precious inputs. All's working perfectly. I'm able to get the output as per mentioned in Head First. Thanks a lot once again.
 
Deepakkumar Devarajan
Ranch Hand
Posts: 54
Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are Welcome.. Life Saver??? hahaha , I understand (Your lead follow up with you for the output).
 
Suvojyoty Saha
Greenhorn
Posts: 25
Firefox Browser Java Oracle
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hehe... no just practicing things on my own. going through the Head First book and trying to improve my skills in Servlets and JSP's

i'm presently facing a new issue....

This is the BeerServlet code

package com.example.web;

import com.example.model.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class BeerServlet extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException
{


BeerExpert be = new BeerExpert();
String str = request.getParameter("color");
List result = be.getResult(str);

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("Beer Selection Advice <br>");

//out.println("<br>Got Beer Color: " + str);

out.print("Values Retrieved: ");
Iterator it = result.iterator();
while(it.hasNext())
{
String s1 = (String) it.next();
out.print(" "+s1);
}
}
}



and below is the BeerExpert code

package com.example.model;

import java.util.*;

public class BeerExpert
{
public List getResult(String value)
{
List al = new ArrayList();
if(value.equals("amber"))
{
al.add("amber");
al.add("found");
}
else
{
al.add("amber");
al.add("not found");
}

return al;
}
}


when i'm executing the folowing command
C:\Documents and Settings\dodo\My Documents\MyProject\beerV1>javac -classpath D:\tomcat\common\lib\servlet-api.jar;classes:. -d classes src/com/example/web/BeerServlet.java

i'm getting the following error

src\com\example\web\BeerServlet.java:3: package com.example.model does not exist

import com.example.model.*;
^
src\com\example\web\BeerServlet.java:15: cannot find symbol
symbol : class BeerExpert
location: class com.example.web.BeerServlet
BeerExpert be = new BeerExpert();
^
src\com\example\web\BeerServlet.java:15: cannot find symbol
symbol : class BeerExpert
location: class com.example.web.BeerServlet
BeerExpert be = new BeerExpert();
^
3 errors


the packages have been named but some how its not able to identify the package...
 
Deepakkumar Devarajan
Ranch Hand
Posts: 54
Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do com folder is a sub folder of beerV1? if not you should tell the compiler where it's placed(Just add it to classpath) . Please note that you have a colon after classes in class path, replace it with ; .
 
Suvojyoty Saha
Greenhorn
Posts: 25
Firefox Browser Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks once again Deepak. On replacing the :. with ;. i got the output..
 
Deepakkumar Devarajan
Ranch Hand
Posts: 54
Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are Welcome.
 
Vijitha Kumara
Bartender
Posts: 3908
9
Chrome Fedora Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suvojyoty,

Next time make sure you use code tags when posting code. You may wrap your code inside [code][/code] tags or use the "Code" button in editing area to do this.
 
Suvojyoty Saha
Greenhorn
Posts: 25
Firefox Browser Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijitha,

Thanks for the info. would surely make a note of using the code tags when posting code.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic